943,742 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1694
  • C# RSS
Nov 21st, 2007
0

Concatination in StringBuilder

Expand Post »
What is the difference in memory allocation and performance by using
C# Syntax (Toggle Plain Text)
  1. StringBuilder.Append("Str1");
  2. StringBuilder.Append("Str2");
  3.  
  4. and
  5.  
  6. StringBuilder.Append("Str1"+"Str2");
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
varagpraveen is offline Offline
3 posts
since Nov 2007
Nov 21st, 2007
0

Re: Concatination in StringBuilder

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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Experiments with StringBuilder (C#)
Next Thread in C# Forum Timeline: Read certain # of lines





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC