StringBuilder

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvin Bruney

    StringBuilder

    On the advice of a user, I've timed stringbuilder v string. Here are the
    results.

    Here are the numbers:
    Total # queries 3747
    Time in Milliseconds

    StringBuilder: String
    460.6624 320.4608
    350.504 220.3168
    240.3456 230.3312
    ----------- -----------
    Ave. 350.504 Ave. 257.0362

    Sample query:
    "Update ds_active_rate SET (rate, surcharge, min_sec, increment_sec,
    begin_date, new_rate, new_rate_date, location_desc, category_id,
    country_code, customer_number , user_name)=('0' ,'0','0','0','2 150-01-01
    12:00:00','','' ,'Afghanistan', 2,93,'1STAMERIC AN','') where rate_deck =
    'COPYRATE' and location_id = 1001 and world_id = 1 and rate_level = 1"

    Specs on my machine
    1Gig SDRAM
    P-IV 2Gig Intel
    20Gig Hard Drive
    Dell C-640

    What I did: I let the routine run till it hit a breakpoint after a timespan
    object. Noted the time, and pointed execution back to the first time object
    and let it run to the break point. This gave me my three times. Obviously,
    some sort of optimization was going on in the background for successive runs
    which accounts for increasingly lower times. Then I converted the code to
    string and repeated the procedure. I don't claim that these numbers are
    scientific. But I did my best to control the process.

    My conclusion is: I think these microsoft guys have been feeding us a bunch
    of bullshit with this stringbuilder efficiency crap.



  • Richard A. Lowe

    #2
    StringBuilder

    Strings are expensive to concatenate as a (partial)
    function of their size... While I can't be sure of what
    kind of test you were doing, if you test concatenation of
    increasingly large strings to one another, the
    StringBuilder can be dozens, if not hundreds of times
    faster than the + operator.

    If you are just concatenating two small strings, then yes,
    the + operator will be faster.

    Richard
    [color=blue]
    >-----Original Message-----
    >On the advice of a user, I've timed stringbuilder v[/color]
    string. Here are the[color=blue]
    >results.
    >
    >Here are the numbers:
    >Total # queries 3747
    >Time in Milliseconds
    >
    >StringBuilde r: String
    >460.6624 320.4608
    >350.504 220.3168
    >240.3456 230.3312
    >----------- -----------
    >Ave. 350.504 Ave. 257.0362
    >
    >Sample query:
    >"Update ds_active_rate SET (rate, surcharge, min_sec,[/color]
    increment_sec,[color=blue]
    >begin_date, new_rate, new_rate_date, location_desc,[/color]
    category_id,[color=blue]
    >country_code , customer_number , user_name)=[/color]
    ('0','0','0','0 ','2150-01-01[color=blue]
    >12:00:00','',' ','Afghanistan' ,2,93,'1STAMERI CAN','')[/color]
    where rate_deck =[color=blue]
    >'COPYRATE' and location_id = 1001 and world_id = 1 and[/color]
    rate_level = 1"[color=blue]
    >
    >Specs on my machine
    >1Gig SDRAM
    >P-IV 2Gig Intel
    >20Gig Hard Drive
    >Dell C-640
    >
    >What I did: I let the routine run till it hit a[/color]
    breakpoint after a timespan[color=blue]
    >object. Noted the time, and pointed execution back to the[/color]
    first time object[color=blue]
    >and let it run to the break point. This gave me my three[/color]
    times. Obviously,[color=blue]
    >some sort of optimization was going on in the background[/color]
    for successive runs[color=blue]
    >which accounts for increasingly lower times. Then I[/color]
    converted the code to[color=blue]
    >string and repeated the procedure. I don't claim that[/color]
    these numbers are[color=blue]
    >scientific. But I did my best to control the process.
    >
    >My conclusion is: I think these microsoft guys have been[/color]
    feeding us a bunch[color=blue]
    >of bullshit with this stringbuilder efficiency crap.
    >
    >
    >
    >.
    >[/color]

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: StringBuilder

      Alvin,[color=blue]
      > My conclusion is: I think these microsoft guys have been feeding us a[/color]
      bunch[color=blue]
      > of bullshit with this stringbuilder efficiency crap.[/color]
      If you are simply building a single update statement then yes the
      StringBuilder maybe like using a shot gun to kill the fly on your foot.

      However if you were to build 1000 update statements as a single batch, then
      the StringBuilder will really shine!

      In other words the first will not perform well (as I believe you
      demonstrated), the second will fly!

      for (I = 0; I < 1000; I++)
      {
      StringBuilder sb = new StringBuilder(" insert(");
      sb.Append("x");
      sb.Append(")");
      String sql = sb.ToString();
      }

      StringBuilder sb = new StringBuilder() ;
      for (i = 0; i < 1000; i++)
      {
      sb.Append("inse rt(");
      sb.Append("x");
      sb.Append(")");
      sb.Append(';'); // end of one statement
      }
      String sql = sb.ToString();

      As in the first I am building 1000 individual commands, presumable to
      execute 1000 individual times. Where as in the second I am building a single
      batch with 1000 commands.

      Forgive the super simple insert statement, I'm demonstrating how to use the
      StringBuilder, not SQL syntax ;-)

      Also irregardless of the timing, you also have temporary string objects to
      consider, in the first case there may be fewer temporary string objects
      created, so although its slightly 'slower' the GC does not need to work as
      hard.

      Don't forget when you allocate the StringBuilder, that you should give the
      capacity value if you know roughly how large the string may get, as this
      improves performance also. (the StringBuilder does not need to reallocate
      its buffer, reallocating the buffer takes time!)
      [color=blue]
      > This gave me my three times. Obviously,
      > some sort of optimization was going on in the background for successive[/color]
      runs[color=blue]
      > which accounts for increasingly lower times.[/color]
      Generally its the JIT compiler the first time through things get 'compiled'
      the second time things are already compiled.

      One last comment, as was discussed earlier (in a different thread), if you
      are using string concatenation for the parameters to StringBuilder.A ppend,
      then you may be negating the benefit of the StringBuilder, as you are
      creating temporary string objects.

      Depending on intended use of the class, I will actually allocate a single
      StringBuilder in the constructor then continue to reuse the single
      StringBuilder. Setting StringBuilder.L ength = 0 each time I started. This
      causes the contents of the buffer to be 'cleared' without allocating a new
      StringBuilder buffer.

      Hope this helps
      Jay

      "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
      message news:ebY1L7dgDH A.4024@TK2MSFTN GP11.phx.gbl...[color=blue]
      > On the advice of a user, I've timed stringbuilder v string. Here are the
      > results.
      >
      > Here are the numbers:
      > Total # queries 3747
      > Time in Milliseconds
      >
      > StringBuilder: String
      > 460.6624 320.4608
      > 350.504 220.3168
      > 240.3456 230.3312
      > ----------- -----------
      > Ave. 350.504 Ave. 257.0362
      >
      > Sample query:
      > "Update ds_active_rate SET (rate, surcharge, min_sec, increment_sec,
      > begin_date, new_rate, new_rate_date, location_desc, category_id,
      > country_code, customer_number , user_name)=('0' ,'0','0','0','2 150-01-01
      > 12:00:00','','' ,'Afghanistan', 2,93,'1STAMERIC AN','') where rate_deck =
      > 'COPYRATE' and location_id = 1001 and world_id = 1 and rate_level = 1"
      >
      > Specs on my machine
      > 1Gig SDRAM
      > P-IV 2Gig Intel
      > 20Gig Hard Drive
      > Dell C-640
      >
      > What I did: I let the routine run till it hit a breakpoint after a[/color]
      timespan[color=blue]
      > object. Noted the time, and pointed execution back to the first time[/color]
      object[color=blue]
      > and let it run to the break point. This gave me my three times. Obviously,
      > some sort of optimization was going on in the background for successive[/color]
      runs[color=blue]
      > which accounts for increasingly lower times. Then I converted the code to
      > string and repeated the procedure. I don't claim that these numbers are
      > scientific. But I did my best to control the process.
      >
      > My conclusion is: I think these microsoft guys have been feeding us a[/color]
      bunch[color=blue]
      > of bullshit with this stringbuilder efficiency crap.
      >
      >
      >[/color]


      Comment

      • Alvin Bruney

        #4
        Re: StringBuilder

        I apologize for not providing the code. The batch command explanation passed
        over my head. I'd like you to explain this a little more. I believe the code
        follows your quidelines. This is a raw cut and paste by the way.

        for(int index = GlobalRateMan.I NITVALUE; index < splitField2.Len gth;
        index++)

        {

        splitField = GlobalRateMan.R egexComma.Split (splitField2[index]);

        if(ForceCreate. Checked)

        strTempString.A ppend(GlobalRat eMan.sqlUpdateA ctiveRate2);

        else

        //insert extra fields

        strTempString.A ppend(GlobalRat eMan.sqlUpdateA ctiveRate1);

        //check for valid record. replace some excel formatting

        if(splitField.L ength == 37)

        {

        newrate = splitField[NEW_RATE];

        newdate = GlobalRateMan.F ormatDate(split Field[NEW_RATE_DATE]);

        //both fields must be entered or neither in the case of updates to field
        like surcharges

        if(newrate.Leng th > 0 && newdate.Length > 0)

        {

        strTempString.A ppend("'").Appe nd(splitField[RATE]).Append("',");

        strTempString.A ppend("'").Appe nd(splitField[SURCHARGE]).Append("',");

        strTempString.A ppend("'").Appe nd(splitField[MIN_SEC]).Append("',");

        strTempString.A ppend("'").Appe nd(splitField[INCR_SEC]).Append("',");

        strTempString.A ppend("'").Appe nd(GlobalRateMa n.FormatDate(sp litField[BEGIN_D
        ATE])).Append("',") ;

        strTempString.A ppend("'").Appe nd(newrate.Repl ace("$",GlobalR ateMan.EMPTY)). A
        ppend("',");

        strTempString.A ppend("'").Appe nd(newdate).App end("',");

        strTempString.A ppend("'").Appe nd(InsertUserNa me().TrimEnd()) .Append("')");


        //option which is checked for the time test

        if(ForceCreate. Checked)

        {

        strTempString.A ppend("'").Appe nd(splitField[8] + "',");

        strTempString.A ppend(splitFiel d[12]).Append(",");

        strTempString.A ppend(splitFiel d[14]).Append(",");

        strTempString.A ppend("'").Appe nd(splitField[2]).Append("',");

        }

        }


        strTempString = strTempString.R eplace("',)","' )");

        if(newrate.Leng th == 0 && newdate.Length == 0)

        strTempString = strTempString.R eplace("new_rat e,new_rate_date ,","");

        //usually 3 guys are in this listbox

        for(int i = 0; i < RateDeckConfirm .Items.Count; i++)

        {

        strTempString2. Length = 0;

        strTempString2. Append(" where rate_deck =
        '").Append(Rate DeckConfirm.Ite ms[i].Text.TrimEnd() ).Append("'");

        strTempString2. Append(" and location_id = ").Append(split Field[16]);

        strTempString2. Append(" and world_id = ").Append(split Field[10]);

        strTempString2. Append(" and rate_level = ").Append(split Field[34]);

        //caught a live one

        sql.Add(strTemp String + strTempString2. ToString());

        }

        strTempString.L ength = 0;

        newrate = newdate = "";

        }

        }

        "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
        news:exT3aaegDH A.2260@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Alvin,[color=green]
        > > My conclusion is: I think these microsoft guys have been feeding us a[/color]
        > bunch[color=green]
        > > of bullshit with this stringbuilder efficiency crap.[/color]
        > If you are simply building a single update statement then yes the
        > StringBuilder maybe like using a shot gun to kill the fly on your foot.
        >
        > However if you were to build 1000 update statements as a single batch,[/color]
        then[color=blue]
        > the StringBuilder will really shine!
        >
        > In other words the first will not perform well (as I believe you
        > demonstrated), the second will fly!
        >
        > for (I = 0; I < 1000; I++)
        > {
        > StringBuilder sb = new StringBuilder(" insert(");
        > sb.Append("x");
        > sb.Append(")");
        > String sql = sb.ToString();
        > }
        >
        > StringBuilder sb = new StringBuilder() ;
        > for (i = 0; i < 1000; i++)
        > {
        > sb.Append("inse rt(");
        > sb.Append("x");
        > sb.Append(")");
        > sb.Append(';'); // end of one statement
        > }
        > String sql = sb.ToString();
        >
        > As in the first I am building 1000 individual commands, presumable to
        > execute 1000 individual times. Where as in the second I am building a[/color]
        single[color=blue]
        > batch with 1000 commands.
        >
        > Forgive the super simple insert statement, I'm demonstrating how to use[/color]
        the[color=blue]
        > StringBuilder, not SQL syntax ;-)
        >
        > Also irregardless of the timing, you also have temporary string objects to
        > consider, in the first case there may be fewer temporary string objects
        > created, so although its slightly 'slower' the GC does not need to work as
        > hard.
        >
        > Don't forget when you allocate the StringBuilder, that you should give the
        > capacity value if you know roughly how large the string may get, as this
        > improves performance also. (the StringBuilder does not need to reallocate
        > its buffer, reallocating the buffer takes time!)
        >[color=green]
        > > This gave me my three times. Obviously,
        > > some sort of optimization was going on in the background for successive[/color]
        > runs[color=green]
        > > which accounts for increasingly lower times.[/color]
        > Generally its the JIT compiler the first time through things get[/color]
        'compiled'[color=blue]
        > the second time things are already compiled.
        >
        > One last comment, as was discussed earlier (in a different thread), if you
        > are using string concatenation for the parameters to StringBuilder.A ppend,
        > then you may be negating the benefit of the StringBuilder, as you are
        > creating temporary string objects.
        >
        > Depending on intended use of the class, I will actually allocate a single
        > StringBuilder in the constructor then continue to reuse the single
        > StringBuilder. Setting StringBuilder.L ength = 0 each time I started. This
        > causes the contents of the buffer to be 'cleared' without allocating a new
        > StringBuilder buffer.
        >
        > Hope this helps
        > Jay
        >
        > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
        > message news:ebY1L7dgDH A.4024@TK2MSFTN GP11.phx.gbl...[color=green]
        > > On the advice of a user, I've timed stringbuilder v string. Here are the
        > > results.
        > >
        > > Here are the numbers:
        > > Total # queries 3747
        > > Time in Milliseconds
        > >
        > > StringBuilder: String
        > > 460.6624 320.4608
        > > 350.504 220.3168
        > > 240.3456 230.3312
        > > ----------- -----------
        > > Ave. 350.504 Ave. 257.0362
        > >
        > > Sample query:
        > > "Update ds_active_rate SET (rate, surcharge, min_sec, increment_sec,
        > > begin_date, new_rate, new_rate_date, location_desc, category_id,
        > > country_code, customer_number , user_name)=('0' ,'0','0','0','2 150-01-01
        > > 12:00:00','','' ,'Afghanistan', 2,93,'1STAMERIC AN','') where rate_deck =
        > > 'COPYRATE' and location_id = 1001 and world_id = 1 and rate_level =[/color][/color]
        1"[color=blue][color=green]
        > >
        > > Specs on my machine
        > > 1Gig SDRAM
        > > P-IV 2Gig Intel
        > > 20Gig Hard Drive
        > > Dell C-640
        > >
        > > What I did: I let the routine run till it hit a breakpoint after a[/color]
        > timespan[color=green]
        > > object. Noted the time, and pointed execution back to the first time[/color]
        > object[color=green]
        > > and let it run to the break point. This gave me my three times.[/color][/color]
        Obviously,[color=blue][color=green]
        > > some sort of optimization was going on in the background for successive[/color]
        > runs[color=green]
        > > which accounts for increasingly lower times. Then I converted the code[/color][/color]
        to[color=blue][color=green]
        > > string and repeated the procedure. I don't claim that these numbers are
        > > scientific. But I did my best to control the process.
        > >
        > > My conclusion is: I think these microsoft guys have been feeding us a[/color]
        > bunch[color=green]
        > > of bullshit with this stringbuilder efficiency crap.
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jon Skeet

          #5
          Re: StringBuilder

          Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
          > On the advice of a user, I've timed stringbuilder v string.[/color]

          Doing what with them? You haven't actually said what operations you're
          performing on them in the first place.

          Note that for doing simple benchmarks like this you might like to use
          my benchmarking framework:

          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


          That's much easier than using breakpoints, and it means you can run
          without the debugger attached (which is going to be much more like
          real-world performance).
          [color=blue]
          > My conclusion is: I think these microsoft guys have been feeding us a bunch
          > of bullshit with this stringbuilder efficiency crap.[/color]

          StringBuilder *is* more efficient for building strings - but as you
          haven't said what you did in your tests, it's rather hard to tell what
          you've actually measured.

          If you really doubt that using StringBuilder is a good idea for
          building up strings, I suggest you try these two programs:

          using System;
          using System.Text;

          public class TestStringBuild er
          {
          public static void Main(string[] args)
          {
          DateTime start = DateTime.Now;
          StringBuilder sb = new StringBuilder() ;
          for (int i=0; i < 100000; i++)
          {
          sb.Append ('x');
          }
          DateTime end = DateTime.Now;
          Console.WriteLi ne (sb.ToString(). Length);
          Console.WriteLi ne (end-start);
          }
          }



          using System;
          using System.Text;

          public class TestString
          {
          public static void Main(string[] args)
          {
          DateTime start = DateTime.Now;
          string s = "";
          for (int i=0; i < 100000; i++)
          {
          s = s+'x';
          }
          DateTime end = DateTime.Now;
          Console.WriteLi ne (s.Length);
          Console.WriteLi ne (end-start);
          }
          }

          --
          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

          • Hasani

            #6
            Re: StringBuilder

            You didn't track the memory usage. which is one of the points of using the
            stringbuilder over string
            "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
            message news:ebY1L7dgDH A.4024@TK2MSFTN GP11.phx.gbl...[color=blue]
            > On the advice of a user, I've timed stringbuilder v string. Here are the
            > results.
            >
            > Here are the numbers:
            > Total # queries 3747
            > Time in Milliseconds
            >
            > StringBuilder: String
            > 460.6624 320.4608
            > 350.504 220.3168
            > 240.3456 230.3312
            > ----------- -----------
            > Ave. 350.504 Ave. 257.0362
            >
            > Sample query:
            > "Update ds_active_rate SET (rate, surcharge, min_sec, increment_sec,
            > begin_date, new_rate, new_rate_date, location_desc, category_id,
            > country_code, customer_number , user_name)=('0' ,'0','0','0','2 150-01-01
            > 12:00:00','','' ,'Afghanistan', 2,93,'1STAMERIC AN','') where rate_deck =
            > 'COPYRATE' and location_id = 1001 and world_id = 1 and rate_level = 1"
            >
            > Specs on my machine
            > 1Gig SDRAM
            > P-IV 2Gig Intel
            > 20Gig Hard Drive
            > Dell C-640
            >
            > What I did: I let the routine run till it hit a breakpoint after a[/color]
            timespan[color=blue]
            > object. Noted the time, and pointed execution back to the first time[/color]
            object[color=blue]
            > and let it run to the break point. This gave me my three times. Obviously,
            > some sort of optimization was going on in the background for successive[/color]
            runs[color=blue]
            > which accounts for increasingly lower times. Then I converted the code to
            > string and repeated the procedure. I don't claim that these numbers are
            > scientific. But I did my best to control the process.
            >
            > My conclusion is: I think these microsoft guys have been feeding us a[/color]
            bunch[color=blue]
            > of bullshit with this stringbuilder efficiency crap.
            >
            >
            >[/color]


            Comment

            • Jon Skeet

              #7
              Re: StringBuilder

              Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=blue]
              > Depending on intended use of the class, I will actually allocate a single
              > StringBuilder in the constructor then continue to reuse the single
              > StringBuilder. Setting StringBuilder.L ength = 0 each time I started. This
              > causes the contents of the buffer to be 'cleared' without allocating a new
              > StringBuilder buffer.[/color]

              I don't believe this is a good idea, personally. Creating a single
              empty StringBuilder is cheap - and the code is more natural. When you
              want to build a string, you create a StringBuilder and use it. When you
              want to build the next string, you create a new StringBuilder. No
              threading worries, no extra member which has no logical reason for
              existing, etc.

              Also, things are likely to stay in generation 0 - why get stuff pushed
              up to higher generations (where write barriers might become an issue)
              unnecessarily?

              Unless I had some reason to think that the StringBuilder code was the
              bottleneck *and* I had profiled both ways of working, I'd use the more
              natural 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

              • Jon Skeet

                #8
                Re: StringBuilder

                Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                > I apologize for not providing the code. The batch command explanation passed
                > over my head. I'd like you to explain this a little more. I believe the code
                > follows your quidelines. This is a raw cut and paste by the way.[/color]

                Yes - if you could change the formatting so it came out looking
                slightly more readable in future, that would be appreciated :)

                You've shown the StringBuilder code, but not the String code.

                I strongly suggest you refactor this code in a way that lets you do a
                straight, repeatable benchmark (i.e. it's a complete program which
                doesn't depend on any external factors, it has all the data constructed
                before the loop is entered, etc) and post both cases of that. Certainly
                based on that code, I would expect it to be significantly faster at
                constructing the queries than straight string concatenations.

                <snip>

                --
                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

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: StringBuilder

                  Jon,
                  Doh! ;-)

                  That's why the statement is prefixed with 'depending on intended use of the
                  class'. Read as 'The StringBuilder is an integral part of the implementation
                  of the class'. In other words a number of methods of the class delegate to a
                  StringBuilder.

                  Similar to how the System.IO.Strin gWriter class wraps a StringBuilder class.

                  Otherwise I agree for most things its not a good idea. I suppose I should
                  have emphasized that more.

                  Thanks for bringing up this caution!

                  Jay


                  "Jon Skeet" <skeet@pobox.co m> wrote in message
                  news:MPG.19da87 6fde6779d998972 4@news.microsof t.com...[color=blue]
                  > Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=green]
                  > > Depending on intended use of the class, I will actually allocate a[/color][/color]
                  single[color=blue][color=green]
                  > > StringBuilder in the constructor then continue to reuse the single
                  > > StringBuilder. Setting StringBuilder.L ength = 0 each time I started.[/color][/color]
                  This[color=blue][color=green]
                  > > causes the contents of the buffer to be 'cleared' without allocating a[/color][/color]
                  new[color=blue][color=green]
                  > > StringBuilder buffer.[/color]
                  >
                  > I don't believe this is a good idea, personally. Creating a single
                  > empty StringBuilder is cheap - and the code is more natural. When you
                  > want to build a string, you create a StringBuilder and use it. When you
                  > want to build the next string, you create a new StringBuilder. No
                  > threading worries, no extra member which has no logical reason for
                  > existing, etc.
                  >
                  > Also, things are likely to stay in generation 0 - why get stuff pushed
                  > up to higher generations (where write barriers might become an issue)
                  > unnecessarily?
                  >
                  > Unless I had some reason to think that the StringBuilder code was the
                  > bottleneck *and* I had profiled both ways of working, I'd use the more
                  > natural code.
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  • Jon Skeet

                    #10
                    Re: StringBuilder

                    Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=blue]
                    > That's why the statement is prefixed with 'depending on intended use of the
                    > class'. Read as 'The StringBuilder is an integral part of the implementation
                    > of the class'. In other words a number of methods of the class delegate to a
                    > StringBuilder.[/color]

                    Ah, to a single StringBuilder. I'd thought you'd meant it would be
                    effectively started afresh each time, and had nothing to do with the
                    logical state of the object.
                    [color=blue]
                    > Similar to how the System.IO.Strin gWriter class wraps a StringBuilder class.[/color]

                    That makes a lot of sense, yes.
                    [color=blue]
                    > Otherwise I agree for most things its not a good idea. I suppose I should
                    > have emphasized that more.[/color]

                    Nah - don't blame yourself for my lack of comprehension skills :)

                    --
                    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

                    • Alvin Bruney

                      #11
                      Re: StringBuilder

                      Now hold on. I said they weren't scientific. I prefaced the results by that.
                      I don't have a lot of time on my hands to go about making sure this will
                      stand up to the hounds of hell. I leave it up to MS for that.

                      What difference does it make what i do with them. The question I was after
                      was which way is faster to build strings. FYI, i take these queries and
                      write to the database. I don't see what is gained by that tidbit.

                      This is a real world program by the way. Not an arbitrary couple lines of
                      code make up. If I can't see results there, and I should, then either I am
                      doing something wrong. In this case point it out so I can fix the code or MS
                      is on a marketing hype with this stringbuilder/.NET crap.



                      "Jon Skeet" <skeet@pobox.co m> wrote in message
                      news:MPG.19da83 d5647df5e498972 3@news.microsof t.com...[color=blue]
                      > Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=green]
                      > > On the advice of a user, I've timed stringbuilder v string.[/color]
                      >
                      > Doing what with them? You haven't actually said what operations you're
                      > performing on them in the first place.
                      >
                      > Note that for doing simple benchmarks like this you might like to use
                      > my benchmarking framework:
                      >
                      > http://www.pobox.com/~skeet/csharp/benchmark.html
                      >
                      > That's much easier than using breakpoints, and it means you can run
                      > without the debugger attached (which is going to be much more like
                      > real-world performance).
                      >[color=green]
                      > > My conclusion is: I think these microsoft guys have been feeding us a[/color][/color]
                      bunch[color=blue][color=green]
                      > > of bullshit with this stringbuilder efficiency crap.[/color]
                      >
                      > StringBuilder *is* more efficient for building strings - but as you
                      > haven't said what you did in your tests, it's rather hard to tell what
                      > you've actually measured.
                      >
                      > If you really doubt that using StringBuilder is a good idea for
                      > building up strings, I suggest you try these two programs:
                      >
                      > using System;
                      > using System.Text;
                      >
                      > public class TestStringBuild er
                      > {
                      > public static void Main(string[] args)
                      > {
                      > DateTime start = DateTime.Now;
                      > StringBuilder sb = new StringBuilder() ;
                      > for (int i=0; i < 100000; i++)
                      > {
                      > sb.Append ('x');
                      > }
                      > DateTime end = DateTime.Now;
                      > Console.WriteLi ne (sb.ToString(). Length);
                      > Console.WriteLi ne (end-start);
                      > }
                      > }
                      >
                      >
                      >
                      > using System;
                      > using System.Text;
                      >
                      > public class TestString
                      > {
                      > public static void Main(string[] args)
                      > {
                      > DateTime start = DateTime.Now;
                      > string s = "";
                      > for (int i=0; i < 100000; i++)
                      > {
                      > s = s+'x';
                      > }
                      > DateTime end = DateTime.Now;
                      > Console.WriteLi ne (s.Length);
                      > Console.WriteLi ne (end-start);
                      > }
                      > }
                      >
                      > --
                      > Jon Skeet - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet
                      > If replying to the group, please do not mail me too[/color]


                      Comment

                      • Alvin Bruney

                        #12
                        Re: StringBuilder

                        True. I haven't tested for that. This was beyond the scope of the exercise.

                        "Hasani" <HJB417@hotmail .c0m> wrote in message
                        news:_F_bb.1582 2$nU6.3116914@t wister.nyc.rr.c om...[color=blue]
                        > You didn't track the memory usage. which is one of the points of using the
                        > stringbuilder over string
                        > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                        > message news:ebY1L7dgDH A.4024@TK2MSFTN GP11.phx.gbl...[color=green]
                        > > On the advice of a user, I've timed stringbuilder v string. Here are the
                        > > results.
                        > >
                        > > Here are the numbers:
                        > > Total # queries 3747
                        > > Time in Milliseconds
                        > >
                        > > StringBuilder: String
                        > > 460.6624 320.4608
                        > > 350.504 220.3168
                        > > 240.3456 230.3312
                        > > ----------- -----------
                        > > Ave. 350.504 Ave. 257.0362
                        > >
                        > > Sample query:
                        > > "Update ds_active_rate SET (rate, surcharge, min_sec, increment_sec,
                        > > begin_date, new_rate, new_rate_date, location_desc, category_id,
                        > > country_code, customer_number , user_name)=('0' ,'0','0','0','2 150-01-01
                        > > 12:00:00','','' ,'Afghanistan', 2,93,'1STAMERIC AN','') where rate_deck =
                        > > 'COPYRATE' and location_id = 1001 and world_id = 1 and rate_level =[/color][/color]
                        1"[color=blue][color=green]
                        > >
                        > > Specs on my machine
                        > > 1Gig SDRAM
                        > > P-IV 2Gig Intel
                        > > 20Gig Hard Drive
                        > > Dell C-640
                        > >
                        > > What I did: I let the routine run till it hit a breakpoint after a[/color]
                        > timespan[color=green]
                        > > object. Noted the time, and pointed execution back to the first time[/color]
                        > object[color=green]
                        > > and let it run to the break point. This gave me my three times.[/color][/color]
                        Obviously,[color=blue][color=green]
                        > > some sort of optimization was going on in the background for successive[/color]
                        > runs[color=green]
                        > > which accounts for increasingly lower times. Then I converted the code[/color][/color]
                        to[color=blue][color=green]
                        > > string and repeated the procedure. I don't claim that these numbers are
                        > > scientific. But I did my best to control the process.
                        > >
                        > > My conclusion is: I think these microsoft guys have been feeding us a[/color]
                        > bunch[color=green]
                        > > of bullshit with this stringbuilder efficiency crap.
                        > >
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Alvin Bruney

                          #13
                          Re: StringBuilder

                          I used the editor to replace .append( with +
                          compiled and fixed the compiler issues. nothing more. nothing less. I have
                          projects due, i can't spend 10 hours preparing a bench mark.
                          [color=blue]
                          > I strongly suggest you refactor this code in a way that lets you do a[/color]

                          I think you are grasping at straws here. This is the same code, one running
                          stringbuilder, the other running string (formatted as described above). I'm
                          not seeing the results that was hyped about stringbuilder straight out of
                          the box. I wouldn't want to COOK the results (refactor or what ever the
                          latest terminology is) to get these results either. If it is as good as they
                          say, I shouldn't be jumping thru hoops to get the results. This isn't C++.

                          My analogy. If car A is faster than car B then car A should be faster on an
                          old dirt road or the autobahn. They shouldn't have to be refactored to race
                          downhill to show these results.


                          "Jon Skeet" <skeet@pobox.co m> wrote in message
                          news:MPG.19da92 f765fb511b98972 7@news.microsof t.com...[color=blue]
                          > Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=green]
                          > > I apologize for not providing the code. The batch command explanation[/color][/color]
                          passed[color=blue][color=green]
                          > > over my head. I'd like you to explain this a little more. I believe the[/color][/color]
                          code[color=blue][color=green]
                          > > follows your quidelines. This is a raw cut and paste by the way.[/color]
                          >
                          > Yes - if you could change the formatting so it came out looking
                          > slightly more readable in future, that would be appreciated :)
                          >
                          > You've shown the StringBuilder code, but not the String code.
                          >
                          > I strongly suggest you refactor this code in a way that lets you do a
                          > straight, repeatable benchmark (i.e. it's a complete program which
                          > doesn't depend on any external factors, it has all the data constructed
                          > before the loop is entered, etc) and post both cases of that. Certainly
                          > based on that code, I would expect it to be significantly faster at
                          > constructing the queries than straight string concatenations.
                          >
                          > <snip>
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • Jon Skeet

                            #14
                            Re: StringBuilder

                            Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                            > I used the editor to replace .append( with +
                            > compiled and fixed the compiler issues. nothing more. nothing less. I have
                            > projects due, i can't spend 10 hours preparing a bench mark.
                            >[color=green]
                            > > I strongly suggest you refactor this code in a way that lets you do a[/color]
                            >
                            > I think you are grasping at straws here.[/color]

                            I'm not. I'm suggesting that posting only *one* of the pieces of code,
                            and not doing scientific tests in the first place, isn't a good way of
                            coming up with the kind of conclusions you're doing.
                            [color=blue]
                            > This is the same code, one running
                            > stringbuilder, the other running string (formatted as described above).[/color]

                            But not as actually shown. I'd like to *see* the code. For one thing
                            then I could come up with some sample tests myself.
                            [color=blue]
                            > I'm
                            > not seeing the results that was hyped about stringbuilder straight out of
                            > the box. I wouldn't want to COOK the results (refactor or what ever the
                            > latest terminology is) to get these results either.[/color]

                            I wasn't suggesting "cooking" anything. I was suggesting that before
                            you start saying that people are lying, you should have some good
                            grounds.
                            [color=blue]
                            > If it is as good as they
                            > say, I shouldn't be jumping thru hoops to get the results. This isn't C++.
                            >
                            > My analogy. If car A is faster than car B then car A should be faster on an
                            > old dirt road or the autobahn. They shouldn't have to be refactored to race
                            > downhill to show these results.[/color]

                            Your analogy is horribly flawed. Car A can be faster than car B, but
                            without timing them properly you won't know that.

                            --
                            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

                            • Jon Skeet

                              #15
                              Re: StringBuilder

                              Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                              > Now hold on. I said they weren't scientific. I prefaced the results by that.[/color]

                              Yes, and then accused MS of lying, in quite strong terms. Doing that
                              without good evidence is a really bad idea, IMO.
                              [color=blue]
                              > I don't have a lot of time on my hands to go about making sure this will
                              > stand up to the hounds of hell. I leave it up to MS for that.
                              >
                              > What difference does it make what i do with them. The question I was after
                              > was which way is faster to build strings. FYI, i take these queries and
                              > write to the database. I don't see what is gained by that tidbit.[/color]

                              Did your tests include the time taken executing the queries? If so, it
                              could easily be the database which was being slower, or something
                              similar. In order to test string vs StringBuilder performance, you
                              should *only* include that.
                              [color=blue]
                              > This is a real world program by the way. Not an arbitrary couple lines of
                              > code make up. If I can't see results there, and I should, then either I am
                              > doing something wrong.[/color]

                              If the bulk of the time is spent doing the database query, then you
                              *wouldn't* see much difference.
                              [color=blue]
                              > In this case point it out so I can fix the code or MS
                              > is on a marketing hype with this stringbuilder/.NET crap.[/color]

                              Or maybe you could accept that your tests were quite possibly entirely
                              bogus.

                              --
                              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...