Which is faster

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • niteshs@yahoo.com

    Which is faster

    Hello all, just checking which method is faster

    string A;
    A = "";

    if (A == string.Empty)
    {
    MessageBox.Show ("a is empty");
    }
    else
    {
    MessageBox.Show ("a is not empty");
    }
    //or is this quicker
    if (A.Equals(""))
    {
    MessageBox.Show ("a is empty");
    }
    else
    {
    MessageBox.Show ("a is not empty");
    }
    The argument here is in the first method we are using the string
    property empty and then the equality == and in scenario 2 we are using
    the object method equals to compare(so inside == we are calling Equals,
    make sense, is == overloading Equals() ?), thus which 1 is
    quicker...debat ing this with sum1 so we tht we'd post it here
    hope we on the right track...
    chow

  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Which is faster

    hi

    The big question is, does it matter that much?

    If the answer is YES, then put the code inside a loop of 10K iterations and
    see which is faster.


    cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    <niteshs@yahoo. com> wrote in message
    news:1116945526 .086283.277520@ g47g2000cwa.goo glegroups.com.. .[color=blue]
    > Hello all, just checking which method is faster
    >
    > string A;
    > A = "";
    >
    > if (A == string.Empty)
    > {
    > MessageBox.Show ("a is empty");
    > }
    > else
    > {
    > MessageBox.Show ("a is not empty");
    > }
    > //or is this quicker
    > if (A.Equals(""))
    > {
    > MessageBox.Show ("a is empty");
    > }
    > else
    > {
    > MessageBox.Show ("a is not empty");
    > }
    > The argument here is in the first method we are using the string
    > property empty and then the equality == and in scenario 2 we are using
    > the object method equals to compare(so inside == we are calling Equals,
    > make sense, is == overloading Equals() ?), thus which 1 is
    > quicker...debat ing this with sum1 so we tht we'd post it here
    > hope we on the right track...
    > chow
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Which is faster

      <niteshs@yahoo. com> wrote:[color=blue]
      > Hello all, just checking which method is faster
      >
      > string A;
      > A = "";
      >
      > if (A == string.Empty)
      > {
      > MessageBox.Show ("a is empty");
      > }
      > else
      > {
      > MessageBox.Show ("a is not empty");
      > }
      > //or is this quicker
      > if (A.Equals(""))
      > {
      > MessageBox.Show ("a is empty");
      > }
      > else
      > {
      > MessageBox.Show ("a is not empty");
      > }
      > The argument here is in the first method we are using the string
      > property empty and then the equality == and in scenario 2 we are using
      > the object method equals to compare(so inside == we are calling Equals,
      > make sense, is == overloading Equals() ?), thus which 1 is
      > quicker...debat ing this with sum1 so we tht we'd post it here
      > hope we on the right track...[/color]

      Well, they have different semantics - the first won't blow up if A is
      null, whereas the second will throw an exception.

      If you're *really* just after speed and already know that A is non-
      null, I believe that using A.Length==0 is faster than either of the
      methods above. However, I would *strongly* urge you not to micro-
      optimise in this way - write the code in whatever way you find most
      readable, and then look at the performance of the system overall, and
      if you need to improve it, find bottlenecks and try to optimise those
      if you have to.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Mattias Sjögren

        #4
        Re: Which is faster

        >Hello all, just checking which method is faster

        I believe

        if ( A.Length == 0 )

        is somewhat faster on current implementations . But unless you're going
        to run this code lots and lots of times, it probably doesn't matter
        and I suggest you write what you find easiest to read.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • Cowboy \(Gregory A. Beamer\)

          #5
          Re: Which is faster

          if(A.Length==0)
          MessageBox.Show ("a is empty");
          else
          MessageBox.Show ("a is not empty.");

          would be faster than either.

          NOTE, however, that none of these incurs a huge amount of overhead. It is
          when you start manipulating strings that you end up with a lot of overhead,
          like:

          string x = String.Empty;

          for(int i=0;i<100;i++)
          {
          x += i.ToString();
          }

          --
          Gregory A. Beamer
          MVP; MCP: +I, SE, SD, DBA

          *************** *************** *************** ****
          Think outside the box!
          *************** *************** *************** ****
          <niteshs@yahoo. com> wrote in message
          news:1116945526 .086283.277520@ g47g2000cwa.goo glegroups.com.. .[color=blue]
          > Hello all, just checking which method is faster
          >
          > string A;
          > A = "";
          >
          > if (A == string.Empty)
          > {
          > MessageBox.Show ("a is empty");
          > }
          > else
          > {
          > MessageBox.Show ("a is not empty");
          > }
          > //or is this quicker
          > if (A.Equals(""))
          > {
          > MessageBox.Show ("a is empty");
          > }
          > else
          > {
          > MessageBox.Show ("a is not empty");
          > }
          > The argument here is in the first method we are using the string
          > property empty and then the equality == and in scenario 2 we are using
          > the object method equals to compare(so inside == we are calling Equals,
          > make sense, is == overloading Equals() ?), thus which 1 is
          > quicker...debat ing this with sum1 so we tht we'd post it here
          > hope we on the right track...
          > chow
          >[/color]


          Comment

          • Frank Hileman

            #6
            Re: Which is faster

            It is interesting that the string.Length == 0 optimization has been added to
            FxCop as a default rule. If you check against "" you will get a warning or
            error from FxCop. It would have been trivial to put the optimization in the
            compiler. Checking Length == 0 is clumsy because you must often first check
            to see if the string is null:

            if (string1 != null && string1.Length == 0)


            Regards,
            Frank Hileman

            check out VG.net: http://www.vgdotnet.com
            Animated vector graphics system
            Integrated Visual Studio .NET graphics editor

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1cfd4f dc2d51d05098c19 9@msnews.micros oft.com...[color=blue]
            > <niteshs@yahoo. com> wrote:
            > If you're *really* just after speed and already know that A is non-
            > null, I believe that using A.Length==0 is faster than either of the
            > methods above. However, I would *strongly* urge you not to micro-
            > optimise in this way - write the code in whatever way you find most
            > readable, and then look at the performance of the system overall, and
            > if you need to improve it, find bottlenecks and try to optimise those
            > if you have to.[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Which is faster

              Frank Hileman <frankhil@no.sp amming.prodiges oftware.com> wrote:[color=blue]
              > It is interesting that the string.Length == 0 optimization has been added to
              > FxCop as a default rule. If you check against "" you will get a warning or
              > error from FxCop. It would have been trivial to put the optimization in the
              > compiler. Checking Length == 0 is clumsy because you must often first check
              > to see if the string is null:
              >
              > if (string1 != null && string1.Length == 0)[/color]

              Absolutely - if you don't know that the string is non-null, it's much
              less readable than other ways.

              I think it's a bad FxCop rule, myself, but there we go...

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Bruce Wood

                #8
                Re: Which is faster

                After 20 years in the programming industry, I have had only one job in
                which this sort of thing mattered: programming telephone switches. That
                type of programming had hard, real-time deadlines that had to be met,
                so every cycle counted. Of course, if I were still doing that today, I
                wouldn't be using C#. That would be silly.

                In every other job I've ever had, one rule reigns supreme: you get far,
                far more speed advantage out of choosing the right design and the right
                data structures than you ever will tweaking code. True, sometimes you
                run a profiler on your code and discover some part of it that could use
                tweaking, but in my experience that is very rare (unless you're in one
                of those intense areas of programming that requires raw speed or has
                real-time deadlines).

                I find it far more common to see guys making little tweaks like this to
                code that simply has the wrong design, or wastes gobs of cycles because
                of the wrong data structure, or something like that. The worst case I
                ever saw was one programmer who was fiddling with her C code to speed
                up an app that spent 30 hours in the database and mere minutes in the
                CPU (no, I'm not kidding). Our DBA rewrote her SQL queries and got the
                program down to 30 minutes (again, no exaggeration). There's a simple
                lesson here: the big gains in execution speed do not come from tweaking
                C# code. At least, not most of the time.

                ....and don't get me started about people (including, inexplicably,
                Microsoft) who feel this urge to use structs instead of classes for
                things like customer records because "structs have less overhead".
                Grrr.... :)

                Comment

                • Altramagnus

                  #9
                  Re: Which is faster

                  Although I agree with some other which has posted reply that,
                  one should not waste time and effort in trying to micro-optimise code this
                  way,
                  personally, I still feel that for academic purpose, one has to know the
                  difference of each method.



                  <niteshs@yahoo. com> wrote in message
                  news:1116945526 .086283.277520@ g47g2000cwa.goo glegroups.com.. .[color=blue]
                  > Hello all, just checking which method is faster
                  >
                  > string A;
                  > A = "";
                  >
                  > if (A == string.Empty)
                  > {
                  > MessageBox.Show ("a is empty");
                  > }
                  > else
                  > {
                  > MessageBox.Show ("a is not empty");
                  > }
                  > //or is this quicker
                  > if (A.Equals(""))
                  > {
                  > MessageBox.Show ("a is empty");
                  > }
                  > else
                  > {
                  > MessageBox.Show ("a is not empty");
                  > }
                  > The argument here is in the first method we are using the string
                  > property empty and then the equality == and in scenario 2 we are using
                  > the object method equals to compare(so inside == we are calling Equals,
                  > make sense, is == overloading Equals() ?), thus which 1 is
                  > quicker...debat ing this with sum1 so we tht we'd post it here
                  > hope we on the right track...
                  > chow
                  >[/color]


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Which is faster

                    Altramagnus <altramagnus@ho tmail.com> wrote:[color=blue]
                    > Although I agree with some other which has posted reply that, one
                    > should not waste time and effort in trying to micro-optimise code
                    > this way, personally, I still feel that for academic purpose, one has
                    > to know the difference of each method.[/color]

                    I think it's very occasionally helpful to know the difference, but I
                    don't think developers really *need* to know. After all, if you're
                    going to include it for this kind of thing, where do you draw the line?
                    Do you think everyone should know the difference between:

                    public class Test
                    {
                    static int x;

                    static
                    {
                    x = 5;
                    }

                    // Other stuff
                    }

                    and

                    public class Test
                    {
                    static int x = 5;

                    // Other stuff
                    }

                    ?

                    How about whether it's faster to call Convert.ToInt32 (string) or
                    Int32.Parse(str ing)?

                    You can spend far too long learning tiny bits of trivia which you never
                    need to know rather than learning these things as and when you need to
                    - i.e. when you've already found that there's a bottleneck which
                    involves that code.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    Working...