Concatination in StringBuilder

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 3
Reputation: varagpraveen is an unknown quantity at this point 
Solved Threads: 0
varagpraveen varagpraveen is offline Offline
Newbie Poster

Concatination in StringBuilder

 
0
  #1
Nov 21st, 2007
What is the difference in memory allocation and performance by using
  1. StringBuilder.Append("Str1");
  2. StringBuilder.Append("Str2");
  3.  
  4. and
  5.  
  6. StringBuilder.Append("Str1"+"Str2");
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Concatination in StringBuilder

 
0
  #2
Nov 21st, 2007
Ok, I wrote a little test application to time this because you got me interested.

Here's the calculation I used (based off your example).

  1. // Test single appends, going 10,000,000 times.
  2. for (int i = 0; i < 10000000; i++)
  3. _SB1.Append("Test");
  4. // Test append + append going 5,000,000 times.
  5. for (int i = 0; i < 10000000/2; i++)
  6. _SB2.Append("Test" + "Test");

The results (in milliseconds):

First Case (single appends)
77ms
71ms
78ms
73ms

Second Case (Append w/ 2 String +'s)
48ms
61ms
57ms
57ms

So, going half the amount of times, doing the same amount of text, adding strings within an append call is quicker than doing multiple single appends.
Last edited by mariocatch; Nov 21st, 2007 at 10:38 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC