SlideShare a Scribd company logo
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 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
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)
• Maximize speed
• Get the most bang for the buck
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.
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.
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
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.
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).
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
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.

More Related Content

What's hot (20)

PPTX
SQL(DDL & DML)
Sharad Dubey
 
PPTX
Java swing
ssuser3a47cb
 
PPTX
Shell scripting
simha.dev.lin
 
PPT
Database Relationships
wmassie
 
PDF
SQL Joins With Examples | Edureka
Edureka!
 
PPTX
Access control list acl - permissions in linux
Sreenatha Reddy K R
 
PPTX
Sql Constraints
I L0V3 CODING DR
 
PDF
Integrity constraints in dbms
Vignesh Saravanan
 
PPTX
Oracle basic queries
PRAKHAR JHA
 
PPTX
Data model and entity relationship
Knowledge Center Computer
 
PPT
Chapter03 Creating And Managing User Accounts
Raja Waseem Akhtar
 
PPT
Pl sql guide
Vinay Kumar
 
PPT
Retrieving data using the sql select statement
Syed Zaid Irshad
 
PPTX
SQL Operators.pptx
RUBAB79
 
PPTX
Procedure n functions
Khadija Parween
 
PPTX
embedded-static-&dynamic
Saranya Natarajan
 
PDF
Inside MongoDB: the Internals of an Open-Source Database
Mike Dirolf
 
PPT
Advanced Sql Training
bixxman
 
PPTX
Array in Java
Ali shah
 
SQL(DDL & DML)
Sharad Dubey
 
Java swing
ssuser3a47cb
 
Shell scripting
simha.dev.lin
 
Database Relationships
wmassie
 
SQL Joins With Examples | Edureka
Edureka!
 
Access control list acl - permissions in linux
Sreenatha Reddy K R
 
Sql Constraints
I L0V3 CODING DR
 
Integrity constraints in dbms
Vignesh Saravanan
 
Oracle basic queries
PRAKHAR JHA
 
Data model and entity relationship
Knowledge Center Computer
 
Chapter03 Creating And Managing User Accounts
Raja Waseem Akhtar
 
Pl sql guide
Vinay Kumar
 
Retrieving data using the sql select statement
Syed Zaid Irshad
 
SQL Operators.pptx
RUBAB79
 
Procedure n functions
Khadija Parween
 
embedded-static-&dynamic
Saranya Natarajan
 
Inside MongoDB: the Internals of an Open-Source Database
Mike Dirolf
 
Advanced Sql Training
bixxman
 
Array in Java
Ali shah
 

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

PPTX
13 Strings and Text Processing
Intro C# Book
 
PPTX
16 strings-and-text-processing-120712074956-phpapp02
Abdul Samee
 
PPT
13 Strings and text processing
maznabili
 
PDF
OOPs difference faqs- 4
Umar Ali
 
PPTX
String in .net
Larry Nung
 
PPT
Csphtp1 15
HUST
 
PDF
LectureNotes-04-DSA
Haitham El-Ghareeb
 
PPTX
13string in c#
Sireesh K
 
PDF
Module 6 - String Manipulation.pdf
MegMeg17
 
PPT
Java performance
Sergey D
 
PPTX
C# String
Raghuveer Guthikonda
 
PPTX
Strings in c#
Dr.Neeraj Kumar Pandey
 
PPTX
Strings in Python For codemonkey training
TermuxHacks
 
PPT
Strings Arrays
phanleson
 
PDF
C# chap 10
Shehrevar Davierwala
 
PDF
C# p6
Renas Rekany
 
PPT
M C6java7
mbruggen
 
PPTX
C# Operators. (C-Sharp Operators)
Abid Kohistani
 
PPTX
String C# - Lec10 (Workshop on C# Programming: Learn to Build)
Jannat Ruma
 
PPTX
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
13 Strings and Text Processing
Intro C# Book
 
16 strings-and-text-processing-120712074956-phpapp02
Abdul Samee
 
13 Strings and text processing
maznabili
 
OOPs difference faqs- 4
Umar Ali
 
String in .net
Larry Nung
 
Csphtp1 15
HUST
 
LectureNotes-04-DSA
Haitham El-Ghareeb
 
13string in c#
Sireesh K
 
Module 6 - String Manipulation.pdf
MegMeg17
 
Java performance
Sergey D
 
Strings in c#
Dr.Neeraj Kumar Pandey
 
Strings in Python For codemonkey training
TermuxHacks
 
Strings Arrays
phanleson
 
M C6java7
mbruggen
 
C# Operators. (C-Sharp Operators)
Abid Kohistani
 
String C# - Lec10 (Workshop on C# Programming: Learn to Build)
Jannat Ruma
 
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
Ad

Recently uploaded (20)

PDF
Printable Icelandic Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Galician Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PPT
Diwali Festival 2025 Bring Home Laxmi Ganesh Marble Idol
SAI SHRADHA MOORTI ART
 
PDF
Printable Indonesian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Greek Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PPTX
Honesty is the best quality that all Christians need to.
DannideVera1
 
PDF
I Have Come Out to Oppose You
Dave546130
 
PDF
Printable Khmer Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Ilocano Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PPSX
Live to Love, Love to Give! (Slideshow by: Kal-el's Shows)
Kal-el's Shows
 
PDF
Printable Irish Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Georgian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
The Odd Women of Seabreeze <3<3<3<3<3<3<3
ssuser83613b
 
PDF
NOTICE OF INFORMATION JC-DKR-07112025-02.pdf
trungvo92
 
PPTX
Chaar Dham Temple in Vrindavan_ A Divine Journey.pptx
chaar dham
 
PDF
Printable Guarani Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Gautama Buddha or Sugata Buddha? Clarifying Vishnu's Avatars.pdf
GovindNath3
 
PDF
Printable Hungarian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Hmong Daw Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Igbo Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Icelandic Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Galician Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Diwali Festival 2025 Bring Home Laxmi Ganesh Marble Idol
SAI SHRADHA MOORTI ART
 
Printable Indonesian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Greek Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Honesty is the best quality that all Christians need to.
DannideVera1
 
I Have Come Out to Oppose You
Dave546130
 
Printable Khmer Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Ilocano Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Live to Love, Love to Give! (Slideshow by: Kal-el's Shows)
Kal-el's Shows
 
Printable Irish Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Georgian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
The Odd Women of Seabreeze <3<3<3<3<3<3<3
ssuser83613b
 
NOTICE OF INFORMATION JC-DKR-07112025-02.pdf
trungvo92
 
Chaar Dham Temple in Vrindavan_ A Divine Journey.pptx
chaar dham
 
Printable Guarani Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Gautama Buddha or Sugata Buddha? Clarifying Vishnu's Avatars.pdf
GovindNath3
 
Printable Hungarian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Hmong Daw Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Igbo Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Ad

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

  • 1. C# String Concatenations (Tested in Unity) Sindharta Tanuwijaya string str = “foo” + “bar”
  • 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
  • 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. 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. 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.
  • 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. 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. 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. 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.