Results 1 to 21 of 21

Thread: Which is faster, an if statement or making something = something else?

  1. #1

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Question Which is faster, an if statement or making something = something else?

    Okay, I know you are confused. Let me be more specific and we'll see if it helps.


    Loop start

    If X = False Then X = True (((((OR)))))) X = True

    Loop end

    With "If X = False Then X = True" it would only make it true once, which seems like it would save time because it wouldn't set X to true every time. But with "X = True" it would just go right ahead and make it true every time, which would seem to save time not having to check to see if X was equal to false every time. So, which one is faster? Checking to see if X = False, or making X = True?
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  2. #2
    New Member
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    7
    Simply X = True is much faster since the processor wouldn't have to jump trough your code.

    X = True is only one CPU instruction, while
    if x=false then x= true needs an extra test.

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    x = true would be faster in my opinion because it doesnt need to check anything
    it just changes the value
    where as the if statement slows down in the loopp

    BEST WAY TO FIND OUT IS test it

    write 2 loops
    time them using GetTickCount API

    i dont have vb installed yet (new format) or i would test it for you

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here is the test
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim lngStart As Long
    5. Dim lngEnd As Long
    6. Dim i As Long
    7. Dim x As Boolean
    8.  
    9. Private Sub cmdEquals_Click()
    10.    x = True
    11.    lngStart = GetTickCount
    12.    
    13.    For i = 1 To 2000000
    14.       x = True
    15.    Next i
    16.    lngEnd = GetTickCount
    17.    
    18. Debug.Print "with x = " & lngEnd - lngStart
    19. End Sub
    20.  
    21. Private Sub cmdIf_Click()
    22.    x = True
    23.    lngStart = GetTickCount
    24.    
    25.    For i = 1 To 2000000
    26.       If x = False Then
    27.          x = True
    28.       End If
    29.    Next i
    30.    lngEnd = GetTickCount
    31.    
    32. Debug.Print "with an if check " & lngEnd - lngStart
    33. End Sub

  5. #5
    New Member
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    7
    The if structure in your second test is never taken; that is not a common situation. better try this:
    Code:
    dim Y as boolean
    For i = 1 To 2000000
          Y = Not Y
          X = Y
          If x = False Then
             x = True
          End If
    Next i
    and
    Code:
    dim Y as boolean
    For i = 1 To 2000000
          Y = Not Y
          X = Y
          x = True
    Next i
    This way, the if structure is taken 1000000 times and 1000000 times not.

  6. #6
    Addicted Member
    Join Date
    Mar 2000
    Posts
    148
    on my computer

    this was 300

    For i = 1 To 2000000
    x = True
    Next i

    ------------------------------------------
    this was 426

    For i = 1 To 2000000
    If x = False Then
    x = True
    End If
    Next i
    -------------------------------------------
    this was 298

    For i = 1 To 2000000
    If x = False Then x = True
    Next i

    so the If x = False Then x = True was faster :-)
    Thanks For All Your Help!
    If I helped you I give God the glory

    From CodeGreen

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    The if structure in your second test is never taken; that is not a common situation. better try this:
    Since when I ran the test, and it if statement was slower even when it was always false, I did not see a need to check further. However your way would probably be more acurate.

  8. #8
    New Member
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    7
    I think the results may change if the test program is compiled;
    The two IF test (single line and multi line) should give the same results.
    If the program isn't compiled, the interpreter has to read 3
    lines instead of one. This is propably time consuming.

  9. #9
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    x = true should be faster. if not, then VB has issues

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  10. #10
    Addicted Member
    Join Date
    Mar 2000
    Posts
    148
    if x is most of the time going to be true then

    If x = False Then x = True

    will be faster

    but if x most of the time is going to be false then

    x = true

    will be faster
    Thanks For All Your Help!
    If I helped you I give God the glory

    From CodeGreen

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    I think you are wrong. Any time vb is going to have to check a value it will probably take longer than to just change it. Either way, if you are only talking a few milliseconds with a loop that runs two million time, what difference does it make.

  12. #12
    Addicted Member
    Join Date
    Mar 2000
    Posts
    148
    after you have compiled it there all about the same :-)
    Thanks For All Your Help!
    If I helped you I give God the glory

    From CodeGreen

  13. #13
    Addicted Member
    Join Date
    Mar 2000
    Posts
    148
    can some one help with this
    Help Here
    Thanks For All Your Help!
    If I helped you I give God the glory

    From CodeGreen

  14. #14
    Tygur
    Guest
    Originally posted by codegreen
    if x is most of the time going to be true then

    If x = False Then x = True

    will be faster

    but if x most of the time is going to be false then

    x = true

    will be faster
    If X is always true, they should take about the same time. If X is ever false, doing it without the If statement would be faster. Since the only reason why you'd use the If statement is because X might be one or the other, it would be more practical to simply put "X = True".

  15. #15
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375

    Re: Which is faster, an if statement or making something = something else?

    Originally posted by krah
    Okay, I know you are confused. Let me be more specific and we'll see if it helps.


    Loop start

    If X = False Then X = True (((((OR)))))) X = True

    Loop end

    With "If X = False Then X = True" it would only make it true once, which seems like it would save time because it wouldn't set X to true every time. But with "X = True" it would just go right ahead and make it true every time, which would seem to save time not having to check to see if X was equal to false every time. So, which one is faster? Checking to see if X = False, or making X = True?
    Something i'm wondering though...If you only check for one thing, then after you find it, shouldn't you break out of the loop? If its a For...Next loop then use Exit For. Not sure about the other loops. Just a thought.
    You just proved that sig advertisements work.

  16. #16

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163
    That would be true if I was only checking for one thing, and it looks like that in my example, but I did that just for the sake of simplicity. There is actually quite a bit more jammed in there.
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  17. #17
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759
    On my computer this is what I got.

    with x = 170
    with an if check 200

    equals seems to be faster than an if statement.

  18. #18
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by krah
    That would be true if I was only checking for one thing, and it looks like that in my example, but I did that just for the sake of simplicity. There is actually quite a bit more jammed in there.
    Ok, cool.
    You just proved that sig advertisements work.

  19. #19
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    I think you need to keep in mind also that windows is multi-tasking. You could probably run that test several times in a row and get different results because the CPU is not dedicated to your test program. Even if no other applications are running windows still has background stuff going on.

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  20. #20
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258
    run this in compiled form and tell the numbers
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Dim lngStart As Long
    5. Dim lngEnd As Long
    6. Dim i As Long
    7. Dim x As Boolean
    8.  
    9. Private Sub com1()
    10.    x = True
    11.    lngStart = GetTickCount
    12.    
    13.    For i = 1 To 2000000
    14.       x = True
    15.    Next i
    16.    lngEnd = GetTickCount
    17.    
    18. Print "with x = " & lngEnd - lngStart
    19. End Sub
    20.  
    21. Private Sub com2()
    22.    x = True
    23.    lngStart = GetTickCount
    24.    
    25.    For i = 1 To 2000000
    26.       If x = False Then
    27.          x = True
    28.       End If
    29.    Next i
    30.    lngEnd = GetTickCount
    31.    
    32. Print "with an if 3 line check " & lngEnd - lngStart
    33. End Sub
    34.  
    35. Private Sub com3()
    36.    x = True
    37.    lngStart = GetTickCount
    38.    
    39.    For i = 1 To 2000000
    40.       If x = False Then x = True
    41.    Next i
    42.    lngEnd = GetTickCount
    43.    
    44. Print "with an if 1 line check " & lngEnd - lngStart
    45. End Sub
    46.  
    47. Private Sub Form_Load()
    48. Form1.Show
    49. DoEvents
    50. com1
    51. com2
    52. com3
    53. End Sub

  21. #21
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    As Tygur said, it is essentially a moot point. If u didnt need to perform the test then u would put x = true BUT if the effect of setting X = true directly causes a noticeable performance downgrade then u would do the test. A good example of that is when u are doing things in a MouseOver event. it may cause noticeable flicker to continually set a label's properties rather than testing whether a property is set or not.
    regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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