Results 1 to 16 of 16

Thread: VB Loops... fastest typ?

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    VB Loops... fastest typ?

    Which is the fastest executing VB loop construct? Do you have a favourite?

    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2
    Fanatic Member dongaman's Avatar
    Join Date
    Aug 2001
    Location
    xi'an
    Posts
    616
    Run below codes
    Code:
    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Command1_Click()
    Dim i, j, k, temp, ret1, ret2, ret3 As Long
    k = 1000000
    ''''''''''''''''''''
    j = GetTickCount()
    For i = 1 To k
    temp = 3
    Next
    'MsgBox "For/Next:" & GetTickCount() - j
    ret1 = GetTickCount() - j
    ''''''''''''''''''''
    i = 1
    j = GetTickCount()
    Do While i <= k
    temp = 3
    i = i + 1
    Loop
    'MsgBox "Do/Loop:" & GetTickCount() - j
    ret2 = GetTickCount() - j
    '''''''''''''''''''''
    i = 1
    j = GetTickCount()
    While i <= k
    temp = 3
    i = i + 1
    Wend
    ret3 = GetTickCount() - j
    MsgBox "For/Next:" & ret1 & vbCrLf & "Do/Loop:" & ret2 & vbCrLf & "While/Wend:" & ret3
    End Sub
    I am just aman.

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    dongaman, the worst part of your code is the Dim statement. It will only cast ret3 as Long, and the rest as Variant

    In VB you have to explicitly declare the data type of each variable, whether you use one Dim or separate Dim statements for them. If you don't declare it explicitly, it's cast as a Variant.

    Where the loop involves a counter to be incremented, a Long type variable is preferred nowadays, since maybe today's processors and OSs can manipulate longs faster than ints.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Fanatic Member dongaman's Avatar
    Join Date
    Aug 2001
    Location
    xi'an
    Posts
    616

    I found Syntax of Dim in MSDN:
    Code:
    Dim [WithEvents] varname[([subscripts])] [As [New] type] [, [WithEvents] varname[([subscripts])] [As [New] type]] . . .
    Sorry for my careless
    I am just aman.

  5. #5
    Addicted Member Mars-Martian's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    185
    just one thing dongaman doesnt really have to do with the question just that when u put in code YOU use [ code ] you should use [ vbcode ]
    Just a liitle hint :P
    First person to be able to get what song is currently playing in Winamp 3.

  6. #6
    Fanatic Member dongaman's Avatar
    Join Date
    Aug 2001
    Location
    xi'an
    Posts
    616
    I am just aman.

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: Well ...

    Originally posted by honeybee
    In VB you have to explicitly declare the data type of each variable, whether you use one Dim or separate Dim statements for them. If you don't declare it explicitly, it's cast as a Variant.
    .
    Thankfully, this counterintuitive structure has been fixed in dot net, and Dim the way Dongaman has it written behaves exactly as you would intuitively think (that is if you werent aware ...). I think everyone who has ever used VB has probably made this mistake ... I know I went quite some time without realizing ...

  8. #8
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Originally posted by dongaman
    Run below codes
    Code:
    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Command1_Click()
    Dim i, j, k, temp, ret1, ret2, ret3 As Long
    k = 1000000
    ''''''''''''''''''''
    j = GetTickCount()
    For i = 1 To k
    temp = 3
    Next
    'MsgBox "For/Next:" & GetTickCount() - j
    ret1 = GetTickCount() - j
    ''''''''''''''''''''
    i = 1
    j = GetTickCount()
    Do While i <= k
    temp = 3
    i = i + 1
    Loop
    'MsgBox "Do/Loop:" & GetTickCount() - j
    ret2 = GetTickCount() - j
    '''''''''''''''''''''
    i = 1
    j = GetTickCount()
    While i <= k
    temp = 3
    i = i + 1
    Wend
    ret3 = GetTickCount() - j
    MsgBox "For/Next:" & ret1 & vbCrLf & "Do/Loop:" & ret2 & vbCrLf & "While/Wend:" & ret3
    End Sub
    To be sure that returned values are correct you need to do the same actions in them loops.
    Therefore the results are incorrect cause in the For/Next loop you're doing much less then in others.

    Add a extra long to it t As long
    Add a line in the for next loop t = t +1
    and your results will be more correct.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  9. #9
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    india
    Posts
    418
    hi,

    for loop fastest as i know

  10. #10
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    The speed of loops depends on:
    1. how fast arithmetic operations and compares proceed on the datatype controlling the loop
    2. Overhead involved in processing control structures

    This tested the fastest on this particular box out of:
    While x > 0: x = x-1
    For x =1 to 1000000
    While x < 1000000 : x = x+ 1
    x = x - 1 : if x > 0 then goto

    Code:
    Dim i As Long
    i = 1000000
    Do while i
            i=i-1
    Loop
    I believe it is because of the type of compare.
    It only requires one opcode to load the register and one to get the result vs two opcodes to load two registers and two for the result. The datatype for all of the operations was a long and either added to or subtracted from.

    For ... next was out of contention all the way thru, as was
    if ... then ... else goto <label>

  11. #11
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    352
    for is da fastest... but that's because it doesn't always have to check the value or w/e... ahh!!! can't think or writ eor w/efds kfksfs d

  12. #12
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Regarding Jim's Boolean compare loop post:

    That is indeed interesting.

    To bad that type of loop wouldnt be flexible enough for most situations (have to downcount your index and cant adjust the lower limit).

    Interesting though ...

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Look here for VBSpeed:


    http://www.xbeat.net/vbspeed/

  14. #14
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by NoteMe
    Look here for VBSpeed:


    http://www.xbeat.net/vbspeed/
    I think your link supports Jims explanation as to why his code example runs faster

    A by-product of the tests above: True-conditions perform faster. So, if you can make assumptions about your conditions, set up the code so that the test returns True.

  15. #15
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I know it's a nice page...

  16. #16
    Fanatic Member dongaman's Avatar
    Join Date
    Aug 2001
    Location
    xi'an
    Posts
    616
    Before i written this codes in a minute,i already know which is the fastest loop.
    Add a line in the for next loop t = t +1
    and your results will be more correct.
    But,I do not think so
    I am just aman.

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