SlideShare una empresa de Scribd logo
1 de 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.

Más contenido relacionado

La actualidad más candente

빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)YEONG-CHEON YOU
 
【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計
【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計
【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計UnityTechnologiesJapan002
 
脱 Excel設計書
脱 Excel設計書脱 Excel設計書
脱 Excel設計書rai
 
ImageJプラグインの作成:序論
ImageJプラグインの作成:序論ImageJプラグインの作成:序論
ImageJプラグインの作成:序論nmaro
 
AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践
AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践
AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践Yoshifumi Kawai
 
20分くらいでわかった気分になれるC++20コルーチン
20分くらいでわかった気分になれるC++20コルーチン20分くらいでわかった気分になれるC++20コルーチン
20分くらいでわかった気分になれるC++20コルーチンyohhoy
 
なぜなにリアルタイムレンダリング
なぜなにリアルタイムレンダリングなぜなにリアルタイムレンダリング
なぜなにリアルタイムレンダリングSatoshi Kodaira
 
Android カスタムROMの作り方
Android カスタムROMの作り方Android カスタムROMの作り方
Android カスタムROMの作り方Masahiro Hidaka
 
Starc RTL設計スタイルガイドの検査道具spyglassの使い方
Starc RTL設計スタイルガイドの検査道具spyglassの使い方Starc RTL設計スタイルガイドの検査道具spyglassの使い方
Starc RTL設計スタイルガイドの検査道具spyglassの使い方Kiyoshi Ogawa
 
Unity開発で使える設計の話+Zenjectの紹介
Unity開発で使える設計の話+Zenjectの紹介Unity開発で使える設計の話+Zenjectの紹介
Unity開発で使える設計の話+Zenjectの紹介torisoup
 
CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜
CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜
CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜gree_tech
 
会社でClojure使ってみて分かったこと
会社でClojure使ってみて分かったこと会社でClojure使ってみて分かったこと
会社でClojure使ってみて分かったことRecruit Technologies
 
[0410 박민근] 기술 면접시 자주 나오는 문제들
[0410 박민근] 기술 면접시 자주 나오는 문제들[0410 박민근] 기술 면접시 자주 나오는 문제들
[0410 박민근] 기술 면접시 자주 나오는 문제들MinGeun Park
 
06_게임엔진구성
06_게임엔진구성06_게임엔진구성
06_게임엔진구성noerror
 
CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説
CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説
CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説Takateru Yamagishi
 
Java開発の強力な相棒として今すぐ使えるGroovy
Java開発の強力な相棒として今すぐ使えるGroovyJava開発の強力な相棒として今すぐ使えるGroovy
Java開発の強力な相棒として今すぐ使えるGroovyYasuharu Nakano
 
C++のビルド高速化について
C++のビルド高速化についてC++のビルド高速化について
C++のビルド高速化についてAimingStudy
 
iOS/Androidアプリエンジニアが理解すべき「Model」の振る舞い
iOS/Androidアプリエンジニアが理解すべき「Model」の振る舞いiOS/Androidアプリエンジニアが理解すべき「Model」の振る舞い
iOS/Androidアプリエンジニアが理解すべき「Model」の振る舞いKen Morishita
 

La actualidad más candente (20)

빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)
 
【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計
【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計
【Unite 2018 Tokyo】60fpsのその先へ!スマホの物量限界に挑んだSTG「アカとブルー」の開発設計
 
脱 Excel設計書
脱 Excel設計書脱 Excel設計書
脱 Excel設計書
 
ImageJプラグインの作成:序論
ImageJプラグインの作成:序論ImageJプラグインの作成:序論
ImageJプラグインの作成:序論
 
AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践
AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践
AWS + Windows(C#)で構築する.NET最先端技術によるハイパフォーマンスウェブアプリケーション開発実践
 
20分くらいでわかった気分になれるC++20コルーチン
20分くらいでわかった気分になれるC++20コルーチン20分くらいでわかった気分になれるC++20コルーチン
20分くらいでわかった気分になれるC++20コルーチン
 
なぜなにリアルタイムレンダリング
なぜなにリアルタイムレンダリングなぜなにリアルタイムレンダリング
なぜなにリアルタイムレンダリング
 
Android カスタムROMの作り方
Android カスタムROMの作り方Android カスタムROMの作り方
Android カスタムROMの作り方
 
Starc RTL設計スタイルガイドの検査道具spyglassの使い方
Starc RTL設計スタイルガイドの検査道具spyglassの使い方Starc RTL設計スタイルガイドの検査道具spyglassの使い方
Starc RTL設計スタイルガイドの検査道具spyglassの使い方
 
Gstreamer Basics
Gstreamer BasicsGstreamer Basics
Gstreamer Basics
 
PostgreSQL Query Cache - "pqc"
PostgreSQL Query Cache - "pqc"PostgreSQL Query Cache - "pqc"
PostgreSQL Query Cache - "pqc"
 
Unity開発で使える設計の話+Zenjectの紹介
Unity開発で使える設計の話+Zenjectの紹介Unity開発で使える設計の話+Zenjectの紹介
Unity開発で使える設計の話+Zenjectの紹介
 
CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜
CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜
CEDEC2015_スマホゲーム開発を支えろ!〜汗と涙のQAエンジニアリング〜
 
会社でClojure使ってみて分かったこと
会社でClojure使ってみて分かったこと会社でClojure使ってみて分かったこと
会社でClojure使ってみて分かったこと
 
[0410 박민근] 기술 면접시 자주 나오는 문제들
[0410 박민근] 기술 면접시 자주 나오는 문제들[0410 박민근] 기술 면접시 자주 나오는 문제들
[0410 박민근] 기술 면접시 자주 나오는 문제들
 
06_게임엔진구성
06_게임엔진구성06_게임엔진구성
06_게임엔진구성
 
CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説
CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説
CUDAのアセンブリ言語基礎のまとめ PTXとSASSの概説
 
Java開発の強力な相棒として今すぐ使えるGroovy
Java開発の強力な相棒として今すぐ使えるGroovyJava開発の強力な相棒として今すぐ使えるGroovy
Java開発の強力な相棒として今すぐ使えるGroovy
 
C++のビルド高速化について
C++のビルド高速化についてC++のビルド高速化について
C++のビルド高速化について
 
iOS/Androidアプリエンジニアが理解すべき「Model」の振る舞い
iOS/Androidアプリエンジニアが理解すべき「Model」の振る舞いiOS/Androidアプリエンジニアが理解すべき「Model」の振る舞い
iOS/Androidアプリエンジニアが理解すべき「Model」の振る舞い
 

Similar a 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
 

Similar a 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
 

Último

Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...baharayali
 
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCRElite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCRDelhi Call girls
 
Genesis 1:8 || Meditate the Scripture daily verse by verse
Genesis 1:8  ||  Meditate the Scripture daily verse by verseGenesis 1:8  ||  Meditate the Scripture daily verse by verse
Genesis 1:8 || Meditate the Scripture daily verse by versemaricelcanoynuay
 
No 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UK
No 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UKNo 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UK
No 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UKAmil Baba Naveed Bangali
 
+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
 
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
 
Deerfoot Church of Christ Bulletin 5 5 24
Deerfoot Church of Christ Bulletin 5 5 24Deerfoot Church of Christ Bulletin 5 5 24
Deerfoot Church of Christ Bulletin 5 5 24deerfootcoc
 
(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...
(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...
(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...Sanjna Singh
 
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️soniya singh
 
black magic specialist amil baba pakistan no 1 Black magic contact number rea...
black magic specialist amil baba pakistan no 1 Black magic contact number rea...black magic specialist amil baba pakistan no 1 Black magic contact number rea...
black magic specialist amil baba pakistan no 1 Black magic contact number rea...Black Magic Specialist
 
Deerfoot Church of Christ Bulletin 4 28 24
Deerfoot Church of Christ Bulletin 4 28 24Deerfoot Church of Christ Bulletin 4 28 24
Deerfoot Church of Christ Bulletin 4 28 24deerfootcoc
 
Study of the Psalms Chapter 1 verse 2 - wanderean
Study of the Psalms Chapter 1 verse 2 - wandereanStudy of the Psalms Chapter 1 verse 2 - wanderean
Study of the Psalms Chapter 1 verse 2 - wandereanmaricelcanoynuay
 
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
 
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️soniya singh
 
Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...
Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...
Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...anilsa9823
 
madina book to learn arabic part1
madina   book   to  learn  arabic  part1madina   book   to  learn  arabic  part1
madina book to learn arabic part1fa3el khair
 
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
 
Lesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptxLesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptxCelso Napoleon
 
Flores de Mayo-history and origin we need to understand
Flores de Mayo-history and origin we need to understandFlores de Mayo-history and origin we need to understand
Flores de Mayo-history and origin we need to understandvillamilcecil909
 
Vashikaran Specialist in London Black Magic Removal No 1 Astrologer in UK
Vashikaran Specialist in London Black Magic Removal No 1 Astrologer in UKVashikaran Specialist in London Black Magic Removal No 1 Astrologer in UK
Vashikaran Specialist in London Black Magic Removal No 1 Astrologer in UKAmil Baba Naveed Bangali
 

Último (20)

Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
 
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCRElite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
Elite Class ➥8448380779▻ Call Girls In Mehrauli Gurgaon Road Delhi NCR
 
Genesis 1:8 || Meditate the Scripture daily verse by verse
Genesis 1:8  ||  Meditate the Scripture daily verse by verseGenesis 1:8  ||  Meditate the Scripture daily verse by verse
Genesis 1:8 || Meditate the Scripture daily verse by verse
 
No 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UK
No 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UKNo 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UK
No 1 Amil baba in UK Best Astrologer in UK Famous Vashikaran Specialist in UK
 
+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...
 
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...
 
Deerfoot Church of Christ Bulletin 5 5 24
Deerfoot Church of Christ Bulletin 5 5 24Deerfoot Church of Christ Bulletin 5 5 24
Deerfoot Church of Christ Bulletin 5 5 24
 
(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...
(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...
(NISHA) Call Girls Sanath Nagar ✔️Just Call 7001035870✔️ HI-Fi Hyderabad Esco...
 
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
call girls in rohini sector 22 Delhi 8264348440 ✅ call girls ❤️
 
black magic specialist amil baba pakistan no 1 Black magic contact number rea...
black magic specialist amil baba pakistan no 1 Black magic contact number rea...black magic specialist amil baba pakistan no 1 Black magic contact number rea...
black magic specialist amil baba pakistan no 1 Black magic contact number rea...
 
Deerfoot Church of Christ Bulletin 4 28 24
Deerfoot Church of Christ Bulletin 4 28 24Deerfoot Church of Christ Bulletin 4 28 24
Deerfoot Church of Christ Bulletin 4 28 24
 
Study of the Psalms Chapter 1 verse 2 - wanderean
Study of the Psalms Chapter 1 verse 2 - wandereanStudy of the Psalms Chapter 1 verse 2 - wanderean
Study of the Psalms Chapter 1 verse 2 - wanderean
 
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 👔
 
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
Call Girls in majnu ka tila Delhi 8264348440 ✅ call girls ❤️
 
Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...
Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...
Lucknow 💋 best call girls in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8...
 
madina book to learn arabic part1
madina   book   to  learn  arabic  part1madina   book   to  learn  arabic  part1
madina book to learn arabic part1
 
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
 
Lesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptxLesson 4 - How to Conduct Yourself on a Walk.pptx
Lesson 4 - How to Conduct Yourself on a Walk.pptx
 
Flores de Mayo-history and origin we need to understand
Flores de Mayo-history and origin we need to understandFlores de Mayo-history and origin we need to understand
Flores de Mayo-history and origin we need to understand
 
Vashikaran Specialist in London Black Magic Removal No 1 Astrologer in UK
Vashikaran Specialist in London Black Magic Removal No 1 Astrologer in UKVashikaran Specialist in London Black Magic Removal No 1 Astrologer in UK
Vashikaran Specialist in London Black Magic Removal No 1 Astrologer in UK
 

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.