Results 1 to 15 of 15

Thread: Which is Faster and Why?

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Which is Faster and Why?

    I already know the Answer but i am just curious to see how well known this fact is about the VB compiler.

    If i write this code 2 different ways which will be faster once it's compiled?


    A)
    VB Code:
    1. If iConditionOne < 1 Then
    2.   If iConditionTwo < 20 Then
    3.       'DoSomething
    4.   End if
    5. End if

    B)
    VB Code:
    1. If iConditionOne < 1 And iConditionTwo < 20 Then 'DoSomething
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489
    The first because it evaluates one at a time?????
    just a guess.

  3. #3
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Without actually KNOWING the answer, I would say the first.

    The second has three evaluations to perform, whereas the first only has two.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Chesterfield, Mi
    Posts
    259
    B is quicker.

    Using method A, VB has to ... dunno if right word " Re-Itterate " Each entry...

    It has to trigger the first value before it even looks at the second one....


    Method B already states the the 2 values together to look for.. whatever..

    I'm right arent i? Just cant explain it very well

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Me thought is such:

    Case A

    Eval First
    Its true
    Eval Second
    Its true
    . . Do processing

    Eval First
    Its False
    Skip over

    Case B

    Eval First
    Its true
    Eval Second
    Its true
    Eval true and true
    Its true
    . . Do processing

    Eval First
    Its False
    Eval Second
    Its True
    Eval true and false
    Its False
    Skip over

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    i would say it doesn't matter unless you have some really complex statements... i don't think you would ever see a difference using the example you put up.. even though i am sure it was just an example... the computer is too fast for you to see any difference.. even an old computer... but you know what i am saying right?

  7. #7
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I say A as well, as VB6 does not support short-circuiting, IIRC.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  8. #8
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Chesterfield, Mi
    Posts
    259
    My version of thought was:

    Method A:

    Search through Everything for First Value
    If true Record value and continue to end
    Search Through Everything for Second Value
    Result

    Method B:

    Search through Everything for First And Second value near-Simaltoneously


    no?

  9. #9
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    A is. Somewhere i read it take more time for VB to evaluate with "and" ... even though it's still eval 2 values.
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  10. #10
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Chesterfield, Mi
    Posts
    259
    fine :P

    I conceed

    it's Eh

  11. #11

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    JoshT got it correct.

    A is faster becasue VB does not use Short Circuiting.

    Short Circuiting means that if ConditionOne evaluates to false then it doesnt even look at the rest of the code. But VB however, does look at the rest of the code on that line.

    This example alone would not make any difference in overall speed of a project. But if you make a habbit of using Optimization techniques such as this throughout an entire project you will indeed notice a difference.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  12. #12
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    That's correct. We've learned at school that the use of nested if statements can increase speed very much! (Not to mistake it for nested loops, which decreases speed exponently (is that correct?))

  13. #13
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    so what does he win???

  14. #14
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Originally posted by Michael_Kamen
    That's correct. We've learned at school that the use of nested if statements can increase speed very much! (Not to mistake it for nested loops, which decreases speed exponently (is that correct?))
    Not really. It's just that you can end up doing many, many loops, which take a while.

  15. #15
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    Originally posted by kleinma
    so what does he win???
    Option A

    But i think you will find it hard to measure any difference, until you're going to use this kind of statements hundreds of times.

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