And vs. AndAlso, performance??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A Traveler

    And vs. AndAlso, performance??

    I was just curious if anyone knows how the combinations of And/AndAlso and
    Or/OrElse compare in terms of performance.
    Which takes more work for the system, performing two evaluations on an And
    or performing short-circuiting on an AndAlso?

    Purely for enlightenment.

    Thanks in advance.

    - Arthur Dent.


  • Imran Koradia

    #2
    Re: And vs. AndAlso, performance??

    Well - both OrElse and AndAlso are short circuit operators. So for OrElse,
    if the first condition is met, it won't even check the second one (which
    could be a lengthy operation). Similarly, for AndAlso, if the first
    condition is not met, it'll break out without even checking the second one.
    So in both the short circuit operations, you're saving on the extra
    condition check when the first one is satisfied (OrElse) or not satisfied
    (AndAlso). So these should give you better performance in general. Note that
    its the expressions that are being evaluated that take the processing time
    and not the operations performed by the operators themselves (which are
    simply boolean operations). Its just the way in which the short-circuit
    operators evaluate which gives you a higher probability of better
    performance.


    hope that helps..
    Imran.

    "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
    news:u%238hQh2u EHA.3948@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > I was just curious if anyone knows how the combinations of And/AndAlso and
    > Or/OrElse compare in terms of performance.
    > Which takes more work for the system, performing two evaluations on an And
    > or performing short-circuiting on an AndAlso?
    >
    > Purely for enlightenment.
    >
    > Thanks in advance.
    >
    > - Arthur Dent.
    >
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: And vs. AndAlso, performance??

      "A Traveler" <hitchhikersgui deto-news@yahoo.com> schrieb:[color=blue]
      > I was just curious if anyone knows how the combinations of And/
      > AndAlso and Or/OrElse compare in terms of performance.
      > Which takes more work for the system, performing two evaluations on an And
      > or performing short-circuiting on an AndAlso?[/color]

      I would prefer 'AndAlso'/'OrElse' when a logical operation should be done.
      Short circuiting is faster in many cases, because often parts of the
      expression don't need to be evaluated.

      --
      Herfried K. Wagner [MVP]
      <URL:http://dotnet.mvps.org/>


      Comment

      • Imran Koradia

        #4
        Re: And vs. AndAlso, performance??

        Well - both OrElse and AndAlso are short circuit operators. So for OrElse,
        if the first condition is met, it won't even check the second one (which
        could be a lengthy operation). Similarly, for AndAlso, if the first
        condition is not met, it'll break out without even checking the second one.
        So in both the short circuit operations, you're saving on the extra
        condition check when the first one is satisfied (OrElse) or not satisfied
        (AndAlso). So these should give you better performance in general. Note that
        its the expressions that are being evaluated that take the processing time
        and not the operations performed by the operators themselves (which are
        simply boolean operations). Its just the way in which the short-circuit
        operators evaluate which gives you a higher probability of better
        performance.


        hope that helps..
        Imran.

        "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
        news:u%238hQh2u EHA.3948@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > I was just curious if anyone knows how the combinations of And/AndAlso and
        > Or/OrElse compare in terms of performance.
        > Which takes more work for the system, performing two evaluations on an And
        > or performing short-circuiting on an AndAlso?
        >
        > Purely for enlightenment.
        >
        > Thanks in advance.
        >
        > - Arthur Dent.
        >
        >[/color]


        Comment

        • A Traveler

          #5
          Re: And vs. AndAlso, performance??

          I dont think i was clear in my question...

          I know _both_ OrElse and AndAlso are short-circuit. But take a case where
          the time to process the individual conditions is negligible and can be
          discounted. Then assume that you will break out on the short-circuit only on
          avg about half the time (or less). Will the Also/Else save time in the long
          run then?
          Im thinking along the lines of database indexes. By basic principles, an
          index is a good thing that improves response time when used on a commonly
          search field, just like short-circuit logic is a faster op in basic
          principle and runs faster. But now, if your db index will only actually hit
          about 50% of the time (or less), it essentially becomes useless (and even a
          hindrance, for the extra work the db does to maintain it).
          Im wondering if the Also/Else operators are affected by any sort of similar
          phenomenon, where the % of hits (or some other factor) may degrade the
          benefit ofr them and possibly even make it so they are actually slower. This
          would, i imagine, have a great deal to do with how they are actually
          implemented in the CLR.

          CheerZ.


          "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
          news:u%238hQh2u EHA.3948@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          >I was just curious if anyone knows how the combinations of And/AndAlso and
          >Or/OrElse compare in terms of performance.
          > Which takes more work for the system, performing two evaluations on an And
          > or performing short-circuiting on an AndAlso?
          >
          > Purely for enlightenment.
          >
          > Thanks in advance.
          >
          > - Arthur Dent.
          >[/color]


          Comment

          • A Traveler

            #6
            Re: And vs. AndAlso, performance??

            I dont think i was clear in my question...

            I know _both_ OrElse and AndAlso are short-circuit. But take a case where
            the time to process the individual conditions is negligible and can be
            discounted. Then assume that you will break out on the short-circuit only on
            avg about half the time (or less). Will the Also/Else save time in the long
            run then?
            Im thinking along the lines of database indexes. By basic principles, an
            index is a good thing that improves response time when used on a commonly
            search field, just like short-circuit logic is a faster op in basic
            principle and runs faster. But now, if your db index will only actually hit
            about 50% of the time (or less), it essentially becomes useless (and even a
            hindrance, for the extra work the db does to maintain it).
            Im wondering if the Also/Else operators are affected by any sort of similar
            phenomenon, where the % of hits (or some other factor) may degrade the
            benefit ofr them and possibly even make it so they are actually slower. This
            would, i imagine, have a great deal to do with how they are actually
            implemented in the CLR.

            CheerZ.


            "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
            news:u%238hQh2u EHA.3948@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            >I was just curious if anyone knows how the combinations of And/AndAlso and
            >Or/OrElse compare in terms of performance.
            > Which takes more work for the system, performing two evaluations on an And
            > or performing short-circuiting on an AndAlso?
            >
            > Purely for enlightenment.
            >
            > Thanks in advance.
            >
            > - Arthur Dent.
            >[/color]


            Comment

            • Ken Halter

              #7
              Re: And vs. AndAlso, performance??

              A Traveler wrote:[color=blue]
              > I dont think i was clear in my question...
              >
              > I know _both_ OrElse and AndAlso are short-circuit. But take a case where
              > the time to process the individual conditions is negligible and can be
              > discounted. Then assume that you will break out on the short-circuit only on
              > avg about half the time (or less). Will the Also/Else save time in the long
              > run then?[/color]

              As an innocent bystander just browsing this group, I have to add...

              "time to process the individual conditions is negligible"

              If that's the case, then there's no benefit to short circuits. When the
              tests do take considerable time, it makes sense. The code below will
              obviously benefit from a short circuit.

              If ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
              ResultsFromInte rnetSearch(Some SearchTerm) > 0 Then

              On the other hand, the code below won't benefit much.

              If X = 1 Or X = 2 Then

              fwiw, you can short circuit VB6 by replacing If/Then's with Select Case's

              --
              Ken Halter - MS-MVP-VB - http://www.vbsight.com
              Please keep all discussions in the groups..

              Comment

              • Ken Halter

                #8
                Re: And vs. AndAlso, performance??

                A Traveler wrote:[color=blue]
                > I dont think i was clear in my question...
                >
                > I know _both_ OrElse and AndAlso are short-circuit. But take a case where
                > the time to process the individual conditions is negligible and can be
                > discounted. Then assume that you will break out on the short-circuit only on
                > avg about half the time (or less). Will the Also/Else save time in the long
                > run then?[/color]

                As an innocent bystander just browsing this group, I have to add...

                "time to process the individual conditions is negligible"

                If that's the case, then there's no benefit to short circuits. When the
                tests do take considerable time, it makes sense. The code below will
                obviously benefit from a short circuit.

                If ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
                ResultsFromInte rnetSearch(Some SearchTerm) > 0 Then

                On the other hand, the code below won't benefit much.

                If X = 1 Or X = 2 Then

                fwiw, you can short circuit VB6 by replacing If/Then's with Select Case's

                --
                Ken Halter - MS-MVP-VB - http://www.vbsight.com
                Please keep all discussions in the groups..

                Comment

                • A Traveler

                  #9
                  Re: And vs. AndAlso, performance??

                  Yes, i am aware that if the conditions are negligible, it doesnt make
                  *practical* sense to bother with short circuiting.
                  I am not looking for a practical answer to a specific problem though. I am
                  simply asking whether there are certain conditions (such as hit percentages)
                  under which an And performs better than AndAlso, due simply to the way _And_
                  is implemented vs. how _AndAlso_ is implemented. And i dont mean for a
                  single run, but over the course of a large number of runs, where sometimes
                  it will short circuit and sometimes it wont.



                  "Ken Halter" <Ken_Halter@Use _Sparingly_Hotm ail.com> wrote in message
                  news:edosEY4uEH A.3808@TK2MSFTN GP15.phx.gbl...[color=blue]
                  >A Traveler wrote:[color=green]
                  >> I dont think i was clear in my question...
                  >>
                  >> I know _both_ OrElse and AndAlso are short-circuit. But take a case where
                  >> the time to process the individual conditions is negligible and can be
                  >> discounted. Then assume that you will break out on the short-circuit only
                  >> on avg about half the time (or less). Will the Also/Else save time in the
                  >> long run then?[/color]
                  >
                  > As an innocent bystander just browsing this group, I have to add...
                  >
                  > "time to process the individual conditions is negligible"
                  >
                  > If that's the case, then there's no benefit to short circuits. When the
                  > tests do take considerable time, it makes sense. The code below will
                  > obviously benefit from a short circuit.
                  >
                  > If ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
                  > ResultsFromInte rnetSearch(Some SearchTerm) > 0 Then
                  >
                  > On the other hand, the code below won't benefit much.
                  >
                  > If X = 1 Or X = 2 Then
                  >
                  > fwiw, you can short circuit VB6 by replacing If/Then's with Select Case's
                  >
                  > --
                  > Ken Halter - MS-MVP-VB - http://www.vbsight.com
                  > Please keep all discussions in the groups..[/color]


                  Comment

                  • A Traveler

                    #10
                    Re: And vs. AndAlso, performance??

                    Yes, i am aware that if the conditions are negligible, it doesnt make
                    *practical* sense to bother with short circuiting.
                    I am not looking for a practical answer to a specific problem though. I am
                    simply asking whether there are certain conditions (such as hit percentages)
                    under which an And performs better than AndAlso, due simply to the way _And_
                    is implemented vs. how _AndAlso_ is implemented. And i dont mean for a
                    single run, but over the course of a large number of runs, where sometimes
                    it will short circuit and sometimes it wont.



                    "Ken Halter" <Ken_Halter@Use _Sparingly_Hotm ail.com> wrote in message
                    news:edosEY4uEH A.3808@TK2MSFTN GP15.phx.gbl...[color=blue]
                    >A Traveler wrote:[color=green]
                    >> I dont think i was clear in my question...
                    >>
                    >> I know _both_ OrElse and AndAlso are short-circuit. But take a case where
                    >> the time to process the individual conditions is negligible and can be
                    >> discounted. Then assume that you will break out on the short-circuit only
                    >> on avg about half the time (or less). Will the Also/Else save time in the
                    >> long run then?[/color]
                    >
                    > As an innocent bystander just browsing this group, I have to add...
                    >
                    > "time to process the individual conditions is negligible"
                    >
                    > If that's the case, then there's no benefit to short circuits. When the
                    > tests do take considerable time, it makes sense. The code below will
                    > obviously benefit from a short circuit.
                    >
                    > If ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
                    > ResultsFromInte rnetSearch(Some SearchTerm) > 0 Then
                    >
                    > On the other hand, the code below won't benefit much.
                    >
                    > If X = 1 Or X = 2 Then
                    >
                    > fwiw, you can short circuit VB6 by replacing If/Then's with Select Case's
                    >
                    > --
                    > Ken Halter - MS-MVP-VB - http://www.vbsight.com
                    > Please keep all discussions in the groups..[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: And vs. AndAlso, performance??

                      Arthur,[color=blue]
                      > Im wondering if the Also/Else operators are affected by any sort of
                      > similar phenomenon, where the % of hits (or some other factor) may degrade
                      > the[/color]
                      I am certain there are cases where this is true, I would expect reversing
                      the order of the operands then improve performance?

                      In other words if the first line below performs poorly then use the second
                      line:

                      If b1 AndAlso b2

                      If b2 AndAlso b1


                      Just as you use a tool, such as SQL Server's Query Analyzer to decide if
                      your DB index is useful or not I would recommend you use a tool, such as CLR
                      Profiler to determine if And/Or verses AndAlso/OrElse is useful in a
                      specific routine.

                      Info on the CLR Profiler:




                      Hope this helps
                      Jay

                      "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
                      news:uu5PAw2uEH A.944@TK2MSFTNG P11.phx.gbl...[color=blue]
                      >I dont think i was clear in my question...
                      >
                      > I know _both_ OrElse and AndAlso are short-circuit. But take a case where
                      > the time to process the individual conditions is negligible and can be
                      > discounted. Then assume that you will break out on the short-circuit only
                      > on avg about half the time (or less). Will the Also/Else save time in the
                      > long run then?
                      > Im thinking along the lines of database indexes. By basic principles, an
                      > index is a good thing that improves response time when used on a commonly
                      > search field, just like short-circuit logic is a faster op in basic
                      > principle and runs faster. But now, if your db index will only actually
                      > hit about 50% of the time (or less), it essentially becomes useless (and
                      > even a hindrance, for the extra work the db does to maintain it).
                      > Im wondering if the Also/Else operators are affected by any sort of
                      > similar phenomenon, where the % of hits (or some other factor) may degrade
                      > the benefit ofr them and possibly even make it so they are actually
                      > slower. This would, i imagine, have a great deal to do with how they are
                      > actually implemented in the CLR.
                      >
                      > CheerZ.
                      >
                      >
                      > "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
                      > news:u%238hQh2u EHA.3948@TK2MSF TNGP10.phx.gbl. ..[color=green]
                      >>I was just curious if anyone knows how the combinations of And/AndAlso and
                      >>Or/OrElse compare in terms of performance.
                      >> Which takes more work for the system, performing two evaluations on an
                      >> And or performing short-circuiting on an AndAlso?
                      >>
                      >> Purely for enlightenment.
                      >>
                      >> Thanks in advance.
                      >>
                      >> - Arthur Dent.
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: And vs. AndAlso, performance??

                        Arthur,
                        In addition to my other comments, you can use ILDASM.EXE to look at the IL
                        created.

                        //000131: Dim b1 As Boolean
                        //000132: Dim b2 As Boolean
                        //000133: If b1 And b2 Then
                        IL_0013: ldloc.0
                        IL_0014: ldloc.1
                        IL_0015: and
                        IL_0016: brfalse.s IL_0018
                        //000134:
                        //000135: End If
                        IL_0018: nop
                        //000136: If b1 AndAlso b2 Then
                        IL_0019: ldloc.0
                        IL_001a: brfalse.s IL_001f
                        IL_001c: ldloc.1
                        IL_001d: brfalse.s IL_001f
                        //000137:
                        //000138: End If
                        IL_001f: nop

                        The other thing to consider is what will the JIT do to the IL when it
                        creates the machine executable code.

                        IMHO this question really falls into the 80/20 rule category. That is 80% of
                        the execution time of your program is spent in 20% of your code. I will
                        optimize (worry about performance, memory consumption) the 20% once that 20%
                        has been identified & proven to be a performance problem via profiling (CLR
                        Profiler is one profiling tool).

                        For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
                        article "Yet Another Optimization Article" at



                        Hope this helps
                        Jay


                        "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
                        news:O6DO1l5uEH A.2804@TK2MSFTN GP14.phx.gbl...[color=blue]
                        > Yes, i am aware that if the conditions are negligible, it doesnt make
                        > *practical* sense to bother with short circuiting.
                        > I am not looking for a practical answer to a specific problem though. I am
                        > simply asking whether there are certain conditions (such as hit
                        > percentages) under which an And performs better than AndAlso, due simply
                        > to the way _And_ is implemented vs. how _AndAlso_ is implemented. And i
                        > dont mean for a single run, but over the course of a large number of runs,
                        > where sometimes it will short circuit and sometimes it wont.
                        >
                        >
                        >
                        > "Ken Halter" <Ken_Halter@Use _Sparingly_Hotm ail.com> wrote in message
                        > news:edosEY4uEH A.3808@TK2MSFTN GP15.phx.gbl...[color=green]
                        >>A Traveler wrote:[color=darkred]
                        >>> I dont think i was clear in my question...
                        >>>
                        >>> I know _both_ OrElse and AndAlso are short-circuit. But take a case
                        >>> where the time to process the individual conditions is negligible and
                        >>> can be discounted. Then assume that you will break out on the
                        >>> short-circuit only on avg about half the time (or less). Will the
                        >>> Also/Else save time in the long run then?[/color]
                        >>
                        >> As an innocent bystander just browsing this group, I have to add...
                        >>
                        >> "time to process the individual conditions is negligible"
                        >>
                        >> If that's the case, then there's no benefit to short circuits. When the
                        >> tests do take considerable time, it makes sense. The code below will
                        >> obviously benefit from a short circuit.
                        >>
                        >> If ResultsFromHard DriveSearch(Som eSearchTerm) > 0 And
                        >> ResultsFromInte rnetSearch(Some SearchTerm) > 0 Then
                        >>
                        >> On the other hand, the code below won't benefit much.
                        >>
                        >> If X = 1 Or X = 2 Then
                        >>
                        >> fwiw, you can short circuit VB6 by replacing If/Then's with Select Case's
                        >>
                        >> --
                        >> Ken Halter - MS-MVP-VB - http://www.vbsight.com
                        >> Please keep all discussions in the groups..[/color]
                        >
                        >[/color]


                        Comment

                        Working...