which would you say is faster

  • 14 years ago

    ok wondered if anyone has an opinion on which is the faster code settle an argument in college

     

    If

    (btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "X") Or (btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "X") Or (btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "X") Then

    If player = False Then 'checks if its one player or 2 player

    MessageBox.Show(

    "Player X wins")

    Else

    MessageBox.Show(

    "YOU WIN!")

    End If

    or

    If

    btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "X" Then

       If

    player = False Then 'checks if its one player or 2 player

       MessageBox.Show(

    "Player X wins")

       Else

       MessageBox.Show(

    "YOU WIN!")

    ElseIf btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "X"Then

       If

    player = False Then 'checks if its one player or 2 player

          MessageBox.Show(

    "Player X wins")

       Else

          MessageBox.Show(

    "YOU WIN!")

    ElseIf btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "X" Then

       If

    player = False Then 'checks if its one player or 2 player

          MessageBox.Show(

    "Player X wins")

       Else

          MessageBox.Show(

    "YOU WIN!")

    End If

    thanks mick

  • 14 years ago

    You've made that code extremely difficult to read.

  • 14 years ago

    ok sorry

    would a sigle if  or or statement   be slower than 3 if else statements is the questions sorry if i posted the code badly

    please forgive the noobie

    ;)

    " we all start at the beginning"

  • 14 years ago

    For testing speed you can use the stop watch class.  It's great for figuring out the most efficient code.  For example:

            Dim sw As New Stopwatch
            sw.start()
            For x As Integer = 1 To 100000

        Next
        sw.stop()
        MsgBox("Ticks elapsed " &amp; sw.ElapsedTicks)</pre>That being said I ran each piece of code in a loop. It looped 10000 times and I ran each test 3 times writing down how many ticks the loop took. Note: I only used the bare if structure.&nbsp; I obviously couldn't display a message box 10000 times.&nbsp; The results are:<pre>Without a Winner:
    

    First Method: 9617458 ticks on average (About 4.5% faster) Second Method: 10075610 ticks on average

    With a Winner (First Row) FYI - I only tested the first row which should give the biggest difference

    First Method: 8536682 ticks on average Second Method: 2422805 ticks on average (About 71% faster)

    This code however I also tested and it performed the fastest.
                If (btn1.Text = "X" AndAlso btn2.Text = "X" AndAlso btn3.Text = "X") _
                OrElse (btn4.Text = "X" AndAlso btn5.Text = "X" AndAlso btn6.Text = "X") _
                OrElse (btn7.Text = "X" AndAlso btn8.Text = "X" AndAlso btn9.Text = "X") Then
                'Show player X won
                End If
    Compared to the faster methods the results are:
    Without a Winner:
    Third Option: 3262610 Ticks on average (About 66% faster)

    With a Winner (First Row) Third Option: 2278090 Ticks on average (About 6% faster)

  • 14 years ago

    Thanks its a pity that i was wrong but had an idea i would be lucky i put no money on it

    thansk for your reply

  • 14 years ago

    I'm glad you didn't put any money on it :)  The reason the single 'if' statement takes longer is because it ends up evaluating the text for each button no matter what.  The second 'if' structure only evaluated the first 3 buttons when there was a winner.  If a winner was in the second row it would have to evaluate 6 buttons, but still fewer then all of them.  The reasoning is this.  If you use 'and' or 'or' every condition is evaluated regardless.  For example the statement: IF 1=2 AND 1=1... will evaluate 1=1 even though we know the condition can't be true because the first part failed.  Similiarly the statement: IF 1=1 OR 3=3... will aslo evaluate 3=3 despite the fact we know the statment must be true after looking at only 1=1.  Knowing this you should be able to walk through the two sets of code and reason why one takes longer then the other.  It's not really the single 'if' statement vs. the 'if...elseif' that determines speed it's how many evaluations each had to make.

    Having said all that, hopefully it made some sense, here is some additional information for you.  The 'andalso' 'orelse' statements evaluate only what's necessary to determine a result.  IF 1=2 andalso 1=1 will only evaluate the 1=2 part because we know if that fails the condition must be false.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”