C# String Concatenations
(Tested in Unity)
Sindharta Tanuwijaya
string str = “foo” + “bar”
Before I start
• These are the conclusions in the previous version of
the slides:
1. Use StringBuilder for performance cri...
Let’s Start
What I want to do
• Minimize memory usage
– Done wrongly, it can consume a lot of
unnecessary memory (yes, that’s right)
•...
There are three methods, AFAIK
• The usual plus concatenation:
– “foo” + “bar”
• string.Format:
– string.Format(“{0} {1}”,...
How I did the comparison
• Prepare 100 one-letter strings.
• Perform 1..100th one-letter string concatenations for each
me...
Plus Concatenation
string.format
:
StringBuilder
Performance: Time
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0.045
1
5
9
13
17
21
25
29
33
37
41
45
49
53
57
61
65
69
7...
Performance: Memory
0
200000
400000
600000
800000
1000000
1200000
1400000
1600000
1800000
1
5
9
13
17
21
25
29
33
37
41
45...
Analyzing a little bit more
• string.Format always slower than StringBuilder. This is
understandable because string.Format...
Conclusion
• Use StringBuilder for performance critical code.
• Use plus concatenation for simple cases.
• In the case non...
Upcoming SlideShare
Loading in …5
×

C# string concatenations in unity (Updated 2014/7/11)

5,680 views

Published on

Testing the performance of three methods of c# string concatenations using Unity

Published in: Spiritual, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,680
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
26
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

C# string concatenations in unity (Updated 2014/7/11)

  1. 1. C# String Concatenations (Tested in Unity) Sindharta Tanuwijaya string str = “foo” + “bar”
  2. 2. Before I start • These are the conclusions in the previous version of the slides: 1. Use StringBuilder for performance critical code ! 2. Never use plus concatenation ! 3. For default usage, I personally recommend string.format 4. Prioritize code readability over performance • Two were wrong, because of inaccurate tests. – I’d like apologize to people who followed my previous conclusions
  3. 3. Let’s Start
  4. 4. What I want to do • Minimize memory usage – Done wrongly, it can consume a lot of unnecessary memory (yes, that’s right) • Maximize speed • Get the most bang for the buck
  5. 5. There are three methods, AFAIK • The usual plus concatenation: – “foo” + “bar” • string.Format: – string.Format(“{0} {1}”, foo.ToString(),bar.ToString()); • StringBuilder – StringBuilder string_builder = new StringBuilder(""); string_builder.append(foo.ToString()); string_builder.append(bar.ToString()); • Some articles on the Internet suggest using StringBuilder, and some suggest using string.Format, but I haven’t found any that tries to compare them. – So, I did it.
  6. 6. How I did the comparison • Prepare 100 one-letter strings. • Perform 1..100th one-letter string concatenations for each method. For example: – “1” + “2” => 2 one-letter string concatenations – “1” + “2” + “3” => 3 one-letter string concatenations • Measure only the concatenation time for each method, which means initialization codes should not be included. – Do the concatenation 1000 times, so that we have meaningful numbers to measure. • Output the results in a text file, visualize in Excel • Full source code can be found here: https://github.com/sindharta/unity-sandbox – Open the StringTest scene.
  7. 7. Plus Concatenation
  8. 8. string.format :
  9. 9. StringBuilder
  10. 10. Performance: Time 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 Concat StringFormat StringBuilder • Plus Concatenation is fast at the beginning, but becomes the slowest when concatenating more than 30 one-letter strings. • StringBuilder is almost always the fastest.
  11. 11. Performance: Memory 0 200000 400000 600000 800000 1000000 1200000 1400000 1600000 1800000 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 Concat StringFormat StringBuilder • Plus concatenation takes a little amount of memory at the beginning, but becomes the most memory consuming method when concatenating more than 30 one- letter strings. • StringBuilder is almost always the one which uses the least amount of memory. • (Should probably wrap the tests in a loop and then average the results, because of the spikes. Anyway I think these results are illustrative enough).
  12. 12. Analyzing a little bit more • string.Format always slower than StringBuilder. This is understandable because string.Format uses StringBuilder internally. • string.Format pays initial cost at the beginning for trying to parse the string format, which pays off when concatenating a lot of strings. • The results of concatenating strings which have more than one letters would be a lot different than the results shown here. – Longer strings would slow down plus concatenations because it needs to allocate more memory to accommodate these longer strings. The impact would be much less for the other two methods. – For string.Format to be able to win against plus concatenation in the case of just two string concatenations, I would guess that the strings must have more than 450 letters (1+2+3+…30), but more accurate test is needed. • Code readability in order: 1. Plus concatenation 2. string.Format 3. StringBuilder
  13. 13. Conclusion • Use StringBuilder for performance critical code. • Use plus concatenation for simple cases. • In the case non-critical but a lot of string concatenations, use string.Format. • When confused, prioritize code readability over performance by default.

×