Results 1 to 5 of 5

Thread: [RESOLVED] [2005] For...Next vs. Do...While

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [RESOLVED] [2005] For...Next vs. Do...While

    When I open my assemblies in Reflector, my for...next loops are rewritten like so:

    Original Loop:
    vb.net Code:
    1. For i As Int32 = 0 To 30 Step 1
    2. ' Do something
    3. Next i

    Rewritten Loop:
    vb.net Code:
    1. Dim i As Integer
    2. Do While (i <= 30)
    3. ' Do something
    4. i += 1
    5. Loop

    I have also seen (and used):
    vb.net Code:
    1. Dim i As Integer
    2. Do
    3. ' Do something
    4. i += 1
    5. Loop While (i <= 30)

    Is there any performance difference between those? Or any difference other than the obvious aesthetic one?
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] For...Next vs. Do...While

    I don't think there should be any significant difference. Although I would recommend using Integer instead of Int32. Because for two reasons:
    1. Integer is faster then Int32
    2. Integer will work in 64bit system but Int32 will throw an exeption.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] For...Next vs. Do...While

    Quote Originally Posted by Troy Lundin
    Is there any performance difference between those? Or any difference other than the obvious aesthetic one?
    No, that's just how For loops get compiled to bytecode.


    Quote Originally Posted by VBDT
    1. Integer is faster then Int32
    As far as I know, Integer is mapped to either Int32 or Int64 depending on the platform, so I can't see how this could be true.

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] For...Next vs. Do...While

    Quote Originally Posted by penagate
    No, that's just how For loops get compiled to bytecode.



    As far as I know, Integer is mapped to either Int32 or Int64 depending on the platform, so I can't see how this could be true.
    Hi penagate, This is from MSDN:
    "The Integer data type provides optimal performance on a 32-bit processor. The other integral types are slower to load and store from and to memory."

  5. #5

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [RESOLVED] [2005] For...Next vs. Do...While

    Are you positive that the "other integral types" it mentions are not Byte, Short and Long?
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width