SlideShare une entreprise Scribd logo
1  sur  13
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.

Contenu connexe

Tendances

6.Resource Exhaustion
6.Resource Exhaustion6.Resource Exhaustion
6.Resource Exhaustionphanleson
 
Managing code quality with SonarQube
Managing code quality with SonarQubeManaging code quality with SonarQube
Managing code quality with SonarQubeRadu Vunvulea
 
Software maintenance Unit5
Software maintenance  Unit5Software maintenance  Unit5
Software maintenance Unit5Mohammad Faizan
 
JAVA DUMP SET.pdf
JAVA DUMP SET.pdfJAVA DUMP SET.pdf
JAVA DUMP SET.pdfanandn24
 
The story of SonarQube told to a DevOps Engineer
The story of SonarQube told to a DevOps EngineerThe story of SonarQube told to a DevOps Engineer
The story of SonarQube told to a DevOps EngineerManu Pk
 
SDLC- concept and models
SDLC- concept and modelsSDLC- concept and models
SDLC- concept and modelsAnjali Arora
 
1. object oriented concepts & principles
1. object oriented concepts & principles 1. object oriented concepts & principles
1. object oriented concepts & principles poonam bora
 
Seguridad Base de Datos sql injection v1.0
Seguridad Base de Datos sql injection v1.0Seguridad Base de Datos sql injection v1.0
Seguridad Base de Datos sql injection v1.0José Moreno
 
AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Inc.
 
Software Engineering- Crisis and Process Models
Software Engineering- Crisis and Process ModelsSoftware Engineering- Crisis and Process Models
Software Engineering- Crisis and Process ModelsNishu Rastogi
 

Tendances (20)

6.Resource Exhaustion
6.Resource Exhaustion6.Resource Exhaustion
6.Resource Exhaustion
 
Managing code quality with SonarQube
Managing code quality with SonarQubeManaging code quality with SonarQube
Managing code quality with SonarQube
 
Oop principles
Oop principlesOop principles
Oop principles
 
COCOMO (Software Engineering)
COCOMO (Software Engineering)COCOMO (Software Engineering)
COCOMO (Software Engineering)
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Software maintenance Unit5
Software maintenance  Unit5Software maintenance  Unit5
Software maintenance Unit5
 
unit testing and debugging
unit testing and debuggingunit testing and debugging
unit testing and debugging
 
JAVA DUMP SET.pdf
JAVA DUMP SET.pdfJAVA DUMP SET.pdf
JAVA DUMP SET.pdf
 
SDLC
SDLCSDLC
SDLC
 
The story of SonarQube told to a DevOps Engineer
The story of SonarQube told to a DevOps EngineerThe story of SonarQube told to a DevOps Engineer
The story of SonarQube told to a DevOps Engineer
 
SDLC- concept and models
SDLC- concept and modelsSDLC- concept and models
SDLC- concept and models
 
1. object oriented concepts & principles
1. object oriented concepts & principles 1. object oriented concepts & principles
1. object oriented concepts & principles
 
Seguridad Base de Datos sql injection v1.0
Seguridad Base de Datos sql injection v1.0Seguridad Base de Datos sql injection v1.0
Seguridad Base de Datos sql injection v1.0
 
sqlmap internals
sqlmap internalssqlmap internals
sqlmap internals
 
Space complexity
Space complexitySpace complexity
Space complexity
 
Scratch Lesson 1
Scratch Lesson 1Scratch Lesson 1
Scratch Lesson 1
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
Software Engineering- Crisis and Process Models
Software Engineering- Crisis and Process ModelsSoftware Engineering- Crisis and Process Models
Software Engineering- Crisis and Process Models
 

Similaire à C# String Concatenation Performance Tested in Unity

IntroPython-Week02-StringsIteration.pptx
IntroPython-Week02-StringsIteration.pptxIntroPython-Week02-StringsIteration.pptx
IntroPython-Week02-StringsIteration.pptxchrisdy932
 
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code AuditingCNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code AuditingSam Bowne
 
Python VS GO
Python VS GOPython VS GO
Python VS GOOfir Nir
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingTiểu Hổ
 
#GDC15 Code Clinic
#GDC15 Code Clinic#GDC15 Code Clinic
#GDC15 Code ClinicMike Acton
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
introduction to server-side scripting
introduction to server-side scriptingintroduction to server-side scripting
introduction to server-side scriptingAmirul Shafeeq
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++Mike Acton
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performancePiotr Przymus
 
Ch 18: Source Code Auditing
Ch 18: Source Code AuditingCh 18: Source Code Auditing
Ch 18: Source Code AuditingSam Bowne
 
High-Performance Python
High-Performance PythonHigh-Performance Python
High-Performance PythonWork-Bench
 
Brixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 RecapBrixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 RecapBasil Bibi
 
Low Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling ExamplesLow Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling ExamplesTanel Poder
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programmingJuggernaut Liu
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptVinayakHospet1
 

Similaire à C# String Concatenation Performance Tested in Unity (20)

IntroPython-Week02-StringsIteration.pptx
IntroPython-Week02-StringsIteration.pptxIntroPython-Week02-StringsIteration.pptx
IntroPython-Week02-StringsIteration.pptx
 
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code AuditingCNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code Auditing
 
Python VS GO
Python VS GOPython VS GO
Python VS GO
 
Introduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimizationIntroduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimization
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
#GDC15 Code Clinic
#GDC15 Code Clinic#GDC15 Code Clinic
#GDC15 Code Clinic
 
Introduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimizationIntroduction to Parallelization ans performance optimization
Introduction to Parallelization ans performance optimization
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
introduction to server-side scripting
introduction to server-side scriptingintroduction to server-side scripting
introduction to server-side scripting
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performance
 
Ch 18: Source Code Auditing
Ch 18: Source Code AuditingCh 18: Source Code Auditing
Ch 18: Source Code Auditing
 
High-Performance Python
High-Performance PythonHigh-Performance Python
High-Performance Python
 
Introduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimizationIntroduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimization
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Gpgpu intro
Gpgpu introGpgpu intro
Gpgpu intro
 
Brixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 RecapBrixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 Recap
 
Low Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling ExamplesLow Level CPU Performance Profiling Examples
Low Level CPU Performance Profiling Examples
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
 

Dernier

肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》
肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》
肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》2tofliij
 
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...Amil Baba Mangal Maseeh
 
Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️
Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️
Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️soniya singh
 
The_Chronological_Life_of_Christ_Part_98_Jesus_Frees_Us
The_Chronological_Life_of_Christ_Part_98_Jesus_Frees_UsThe_Chronological_Life_of_Christ_Part_98_Jesus_Frees_Us
The_Chronological_Life_of_Christ_Part_98_Jesus_Frees_UsNetwork Bible Fellowship
 
CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service 👔
CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service  👔CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service  👔
CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service 👔anilsa9823
 
No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Karachi
No.1 Amil baba in Pakistan amil baba in Lahore amil baba in KarachiNo.1 Amil baba in Pakistan amil baba in Lahore amil baba in Karachi
No.1 Amil baba in Pakistan amil baba in Lahore amil baba in KarachiAmil Baba Mangal Maseeh
 
Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...
Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...
Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...Amil Baba Company
 
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCRElite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCRDelhi Call girls
 
الإبانة الصغرى للإمام لابن بطة العكبري الحنبلي
الإبانة الصغرى للإمام لابن بطة العكبري الحنبليالإبانة الصغرى للإمام لابن بطة العكبري الحنبلي
الإبانة الصغرى للإمام لابن بطة العكبري الحنبليJoEssam
 
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶anilsa9823
 
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...anilsa9823
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | Delhisoniya singh
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | Delhisoniya singh
 
Codex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca SapientiaCodex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca Sapientiajfrenchau
 
Famous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jadu
Famous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jaduFamous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jadu
Famous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jaduAmil Baba Naveed Bangali
 
madina book to learn arabic part1
madina   book   to  learn  arabic  part1madina   book   to  learn  arabic  part1
madina book to learn arabic part1JoEssam
 
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...Amil Baba Mangal Maseeh
 

Dernier (20)

肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》
肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》
肄业证书结业证书《德国汉堡大学成绩单修改》Q微信741003700提供德国文凭照片可完整复刻汉堡大学毕业证精仿版本《【德国毕业证书】{汉堡大学文凭购买}》
 
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
 
Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️
Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️
Call Girls in sarojini nagar Delhi 8264348440 ✅ call girls ❤️
 
The_Chronological_Life_of_Christ_Part_98_Jesus_Frees_Us
The_Chronological_Life_of_Christ_Part_98_Jesus_Frees_UsThe_Chronological_Life_of_Christ_Part_98_Jesus_Frees_Us
The_Chronological_Life_of_Christ_Part_98_Jesus_Frees_Us
 
CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service 👔
CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service  👔CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service  👔
CALL ON ➥8923113531 🔝Call Girls Singar Nagar Lucknow best Night Fun service 👔
 
No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Karachi
No.1 Amil baba in Pakistan amil baba in Lahore amil baba in KarachiNo.1 Amil baba in Pakistan amil baba in Lahore amil baba in Karachi
No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Karachi
 
Rohini Sector 21 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 21 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 21 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 21 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...
Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...
Real Amil baba in Pakistan Real NO1 Amil baba Kala Jado Amil baba RAwalpindi ...
 
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCRElite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Naraina Delhi NCR
 
English - The Story of Ahikar, Grand Vizier of Assyria.pdf
English - The Story of Ahikar, Grand Vizier of Assyria.pdfEnglish - The Story of Ahikar, Grand Vizier of Assyria.pdf
English - The Story of Ahikar, Grand Vizier of Assyria.pdf
 
الإبانة الصغرى للإمام لابن بطة العكبري الحنبلي
الإبانة الصغرى للإمام لابن بطة العكبري الحنبليالإبانة الصغرى للإمام لابن بطة العكبري الحنبلي
الإبانة الصغرى للإمام لابن بطة العكبري الحنبلي
 
Call Girls In CP 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In CP 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In CP 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In CP 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service  🕶
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best Female service 🕶
 
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
Lucknow 💋 (Call Girls) in Lucknow | Book 8923113531 Extreme Naughty Call Girl...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Punjabi Bagh | Delhi
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Chirag Delhi | Delhi
 
Codex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca SapientiaCodex Singularity: Search for the Prisca Sapientia
Codex Singularity: Search for the Prisca Sapientia
 
Famous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jadu
Famous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jaduFamous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jadu
Famous No1 Amil baba in UK/Australia, Canada, Germany Amil baba Kala jadu
 
madina book to learn arabic part1
madina   book   to  learn  arabic  part1madina   book   to  learn  arabic  part1
madina book to learn arabic part1
 
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
+92343-7800299 No.1 Amil baba in Pakistan amil baba in Lahore amil baba in Ka...
 

C# String Concatenation Performance Tested in Unity

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