| | |
Concatination in StringBuilder
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
What is the difference in memory allocation and performance by using
C# Syntax (Toggle Plain Text)
StringBuilder.Append("Str1"); StringBuilder.Append("Str2"); and StringBuilder.Append("Str1"+"Str2");
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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).
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.
Here's the calculation I used (based off your example).
C# Syntax (Toggle Plain Text)
// Test single appends, going 10,000,000 times. for (int i = 0; i < 10000000; i++) _SB1.Append("Test"); // Test append + append going 5,000,000 times. for (int i = 0; i < 10000000/2; i++) _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.
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: Experiments with StringBuilder (C#)
- Next Thread: Read certain # of lines
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





