Results 1 to 9 of 9

Thread: Loops, a basic question of performance

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    Loops, a basic question of performance

    Having this as refrence:
    VB Code:
    1. Dim i as integer
    2. For i=0 to 1000
    3. 'Some code here
    4. Next
    Which one of the following will run faster? any as fast as the above?
    1:
    VB Code:
    1. Dim i as Integer, J as Integer=1000
    2. For i=0 to J
    3. 'Some code with no change in J value
    4. Next
    2:
    VB Code:
    1. Dim i as Integer, J as Integer=2000
    2. For i=0 to J/2 ' Or any other operation on J
    3. 'Some code with no change in J value
    4. Next
    3:
    VB Code:
    1. Dim i as Integer
    2. Const J as Integer=1000
    3. For i=0 to J
    4. 'Some code with no change in J value
    5. Next
    4:
    VB Code:
    1. Dim i as Integer
    2. Const J as Integer=2000
    3. For i=0 to J/2 'or any other operation on J
    4. 'Some code with no change in J value
    5. Next

  2. #2
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    I know "1:" is faster than the reference, because it uses the value in cache.
    I can't be absolutely sure about the other examples, though.
    Make a search on optimiziation, I remember seeing a thread about this in VB General some months ago.

  3. #3
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    Why won't you time it?
    Iouri Boutchkine

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Well, timing is a good idea, but i liked to know the underlying process of each one.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Here is the result of some timings:

    considering the following methods:

    Method1:
    For I=0 to 1000000

    Method2:
    Dim J as Integer=1000000
    For I=0 to J

    Method3:
    Dim J as Integer=20000000
    For I=0 to J/2

    Method4:
    Const J as Integer=10000000
    For I=0 to J

    Method5:
    Const J as Integer=20000000
    For I= 0 to J/2

    Results of each round is the result of going through each of the above loops 1000 times and calculating the average time measured in Ticks.

    Round 1 2 3 Avg
    -----------------------------------------------------------------------------------
    Method1 393265.488 397070.960 394467.216 394934.5547
    Method2 564712.016 564912.304 558302.800 562642.3733
    Method3 468673.920 467372.048 463766.864 466604.2773
    Method4 481392.208 480390.768 477286.304 479689.76
    Method5 392664.624 390862.032 390962.176 391496.2773
    -----------------------------------------------------------------------------------

    Somehow strange results, no?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    No way!

  7. #7
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    They aren't accurate results; you can't change the loop amount, they _all_ must remain the same.
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Wold you please explain more? What you mean by loop amount? I know they can't be accurate but they are measured while the computer ran no extra application (besides system and some user underlying processes). However for sure the times to do a loop will vary depending on the system process that run each while.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    Never mind, I didn't realise you where dividing them.
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

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