TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# StringBuilder Append Performance

Benchmark the StringBuilder Append method, testing Append on different string lengths.
StringBuilder Append. StringBuilder optimizes string appends. However, it does not eliminate the cost of individual appends. This benchmark shows that performance improves by appending fewer, larger strings. Appending many smaller strings takes longer overall.StringBuilderOptimization
Benchmark. Let's look at how the data was collected. The goal was to create a StringBuilder with five million characters in it. We append individual strings of lengths 1 to 99 characters to reach that final length. The Append method calls are timed.
C# program that times StringBuilder appends using System; using System.Diagnostics; using System.Text; class Program { const int _length = 5000000; const int _max = 1000000; static void Main() { StringBuilder builder = new StringBuilder(50000 + 100); for (int a = 1; a < 100; a++) { string v = new string('x', a); int repeat = _length / a; var s1 = Stopwatch.StartNew(); for (int i = 0; i < repeat; i++) { builder.Append(v); } s1.Stop(); builder.Clear(); GC.Collect(); Console.WriteLine("{0} chars x {1}: {2:0.00 ms}", a, repeat, ((double)(s1.Elapsed.TotalMilliseconds))); } Console.WriteLine("[Done]"); Console.Read(); } } Output 1 chars x 5000000: 47.02 ms 2 chars x 2500000: 21.26 ms 3 chars x 1666666: 16.93 ms 4 chars x 1250000: 12.35 ms 5 chars x 1000000: 10.72 ms 6 chars x 833333: 9.92 ms 7 chars x 714285: 8.20 ms 8 chars x 625000: 6.80 ms 9 chars x 555555: 8.24 ms 10 chars x 500000: 6.06 ms 11 chars x 454545: 6.54 ms 12 chars x 416666: 5.29 ms 13 chars x 384615: 6.43 ms 14 chars x 357142: 4.90 ms 15 chars x 333333: 5.77 ms 16 chars x 312500: 4.63 ms 17 chars x 294117: 4.58 ms 18 chars x 277777: 4.40 ms 19 chars x 263157: 4.32 ms 20 chars x 250000: 4.25 ms 21 chars x 238095: 4.61 ms 22 chars x 227272: 4.06 ms 23 chars x 217391: 4.74 ms 24 chars x 208333: 3.88 ms 25 chars x 200000: 4.30 ms 26 chars x 192307: 4.45 ms 27 chars x 185185: 4.40 ms 28 chars x 178571: 4.29 ms 29 chars x 172413: 9.04 ms 30 chars x 166666: 4.25 ms 31 chars x 161290: 4.41 ms 32 chars x 156250: 3.65 ms 33 chars x 151515: 4.34 ms 34 chars x 147058: 4.59 ms 35 chars x 142857: 4.40 ms 36 chars x 138888: 4.19 ms 37 chars x 135135: 4.92 ms 38 chars x 131578: 4.31 ms 39 chars x 128205: 4.49 ms 40 chars x 125000: 4.50 ms 41 chars x 121951: 4.36 ms 42 chars x 119047: 4.71 ms 43 chars x 116279: 4.03 ms 44 chars x 113636: 3.61 ms 45 chars x 111111: 4.30 ms 46 chars x 108695: 4.47 ms 47 chars x 106382: 4.54 ms 48 chars x 104166: 3.62 ms 49 chars x 102040: 4.10 ms 50 chars x 100000: 4.15 ms 51 chars x 98039: 4.10 ms 52 chars x 96153: 3.52 ms 53 chars x 94339: 4.10 ms 54 chars x 92592: 4.09 ms 55 chars x 90909: 4.27 ms 56 chars x 89285: 4.11 ms 57 chars x 87719: 4.20 ms 58 chars x 86206: 4.23 ms 59 chars x 84745: 4.25 ms 60 chars x 83333: 4.44 ms 61 chars x 81967: 4.22 ms 62 chars x 80645: 4.26 ms 63 chars x 79365: 4.26 ms 64 chars x 78125: 4.22 ms 65 chars x 76923: 4.22 ms 66 chars x 75757: 4.27 ms 67 chars x 74626: 4.20 ms 68 chars x 73529: 4.32 ms 69 chars x 72463: 3.73 ms 70 chars x 71428: 4.18 ms 71 chars x 70422: 4.48 ms 72 chars x 69444: 3.66 ms 73 chars x 68493: 4.28 ms 74 chars x 67567: 3.77 ms 75 chars x 66666: 4.11 ms 76 chars x 65789: 4.31 ms 77 chars x 64935: 3.82 ms 78 chars x 64102: 4.30 ms 79 chars x 63291: 3.77 ms 80 chars x 62500: 4.19 ms 81 chars x 61728: 3.92 ms 82 chars x 60975: 4.76 ms 83 chars x 60240: 4.21 ms 84 chars x 59523: 4.08 ms 85 chars x 58823: 4.32 ms 86 chars x 58139: 4.30 ms 87 chars x 57471: 4.40 ms 88 chars x 56818: 4.59 ms 89 chars x 56179: 4.30 ms 90 chars x 55555: 4.40 ms 91 chars x 54945: 4.26 ms 92 chars x 54347: 3.86 ms 93 chars x 53763: 4.38 ms 94 chars x 53191: 3.54 ms 95 chars x 52631: 3.75 ms 96 chars x 52083: 4.45 ms 97 chars x 51546: 4.13 ms 98 chars x 51020: 4.20 ms 99 chars x 50505: 4.48 ms
Discussion. How does this benchmark help in real programs? If you have a program that calls Append on strings with fewer than about 20 characters, it could be made faster by using 20 or more characters.StringBuilder Append

Also: There is no benefit to using more than 20 characters at a time. Combining Append calls could make a performance difference.

Summary. Programs that use StringBuilder will perform faster if some calls to Append are combined if possible. Coalescing Append calls could lead to a performance gain. However, this only helps when appending strings less than about 20 characters.StringBuilder Data Types
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf