|
| 1 | +<!DOCTYPE html> |
| 2 | +<!--[if IE]><![endif]--> |
| 3 | +<html> |
| 4 | + |
| 5 | + <head> |
| 6 | + <meta charset="utf-8"> |
| 7 | + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 8 | + <title>Comparison | ValueStringBuilder </title> |
| 9 | + <meta name="viewport" content="width=device-width"> |
| 10 | + <meta name="title" content="Comparison | ValueStringBuilder "> |
| 11 | + <meta name="generator" content="docfx 2.59.4.0"> |
| 12 | + <meta name="description" content="The ValueStringBuilder is a fast and low allocating StringBuilder meant for scenarios where every allocation and millisecond is important."> |
| 13 | + <link rel="shortcut icon" href="../images/logo.png"> |
| 14 | + <link rel="stylesheet" href="../styles/docfx.vendor.css"> |
| 15 | + <link rel="stylesheet" href="../styles/docfx.css"> |
| 16 | + <link rel="stylesheet" href="../styles/main.css"> |
| 17 | + <meta property="docfx:navrel" content="../toc.html"> |
| 18 | + <meta property="docfx:tocrel" content="toc.html"> |
| 19 | + |
| 20 | + <meta property="docfx:rel" content="../"> |
| 21 | + |
| 22 | + </head> |
| 23 | + <body data-spy="scroll" data-target="#affix" data-offset="120"> |
| 24 | + <div id="wrapper"> |
| 25 | + <header> |
| 26 | + |
| 27 | + <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation"> |
| 28 | + <div class="container"> |
| 29 | + <div class="navbar-header"> |
| 30 | + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar"> |
| 31 | + <span class="sr-only">Toggle navigation</span> |
| 32 | + <span class="icon-bar"></span> |
| 33 | + <span class="icon-bar"></span> |
| 34 | + <span class="icon-bar"></span> |
| 35 | + </button> |
| 36 | + |
| 37 | + <a class="navbar-brand" href="../index.html"> |
| 38 | + <img id="logo" class="svg" src="../images/logo.png" alt="ValueStringBuilder"> |
| 39 | + </a> |
| 40 | + </div> |
| 41 | + <div class="collapse navbar-collapse" id="navbar"> |
| 42 | + <form class="navbar-form navbar-right" role="search" id="search"> |
| 43 | + <div class="form-group"> |
| 44 | + <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off"> |
| 45 | + </div> |
| 46 | + </form> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + </nav> |
| 50 | + </header> |
| 51 | + <div class="container body-content"> |
| 52 | + |
| 53 | + <div id="search-results"> |
| 54 | + <div class="search-list">Search Results for <span></span></div> |
| 55 | + <div class="sr-items"> |
| 56 | + <p><i class="glyphicon glyphicon-refresh index-loading"></i></p> |
| 57 | + </div> |
| 58 | + <ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + <div role="main" class="container body-content hide-when-search"> |
| 62 | + |
| 63 | + <div class="sidenav hide-when-search"> |
| 64 | + <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a> |
| 65 | + <div class="sidetoggle collapse" id="sidetoggle"> |
| 66 | + <div id="sidetoc"></div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + <div class="article row grid-right"> |
| 70 | + <div class="col-md-10"> |
| 71 | + <article class="content wrap" id="_content" data-uid="comparison"> |
| 72 | +<h1 id="comparison">Comparison</h1> |
| 73 | + |
| 74 | +<p>The following document will show some key differences between the <code>ValueStringBuilder</code> and similar working string builder like the one from .NET itself.</p> |
| 75 | +<h2 id="systemtextstringbuilder">System.Text.StringBuilder</h2> |
| 76 | +<p>The <code>StringBuilder</code> shipped with the .NET Framework itself is a all-purpose string builder which allows a versatile use. <code>ValueStringBuilder</code> tries to mimic the API as much as possible so developers can adopt the <code>ValueStringBuilder</code> easily where it makes sense. In the following part <code>StringBuilder</code> refers to <code>System.Text.StringBuilder</code>.</p> |
| 77 | +<p><strong>Key differences</strong>:</p> |
| 78 | +<ul> |
| 79 | +<li><code>StringBuilder</code> is a class and does not have the restrictions coming with a <code>ref struct</code>. To know more head over to the <a class="xref" href="known_limitations.html">known limitations</a> section.</li> |
| 80 | +<li><code>StringBuilder</code> works not on <code>Span<T></code> but more on <code>string</code>s or <code>char</code>s. Sometimes even with pointers</li> |
| 81 | +<li><code>StringBuilder</code> uses chunks to represent the string, which the larger the string gets, the better it can perform. <code>ValueStringBuilder</code> only has one internal <code>Span</code> as representation which can cause fragmentation on very big strings.</li> |
| 82 | +<li><code>StringBuilder</code> has a richer API as the <code>ValueStringBuilder</code>. In the future they should have the same amount of API's as the <code>StringBuilder</code> is the "big brother" of this package.</li> |
| 83 | +<li><code>ValueStringBuilder</code> has different API calls like <a href="xref:LinkDotNet.StringBuilder.ValueStringBuilder.IndexOf(ReadOnlySpan%7BSystem.Char%7D)"><code>IndexOf</code></a> or <a href="xref:LinkDotNet.StringBuilder.ValueStringBuilder.LastIndexOf(ReadOnlySpan%7BSystem.Char%7D)"><code>LastIndexOf</code></a>.</li> |
| 84 | +</ul> |
| 85 | +<h2 id="benchmark">Benchmark</h2> |
| 86 | +<p>The following table gives you a small comparison between the <code>StringBuilder</code> which is part of .NET and the <code>ValueStringBuilder</code>:</p> |
| 87 | +<pre><code>BenchmarkDotNet v0.14.0, macOS Sequoia 15.3.1 (24D70) [Darwin 24.3.0] |
| 88 | +Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores |
| 89 | +.NET SDK 9.0.200 |
| 90 | + [Host] : .NET 9.0.2 (9.0.225.6610), Arm64 RyuJIT AdvSIMD |
| 91 | + DefaultJob : .NET 9.0.2 (9.0.225.6610), Arm64 RyuJIT AdvSIMD |
| 92 | + |
| 93 | + |
| 94 | +| Method | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio | |
| 95 | +|-------------------- |----------:|---------:|---------:|------:|-------:|----------:|------------:| |
| 96 | +| DotNetStringBuilder | 126.74 ns | 0.714 ns | 0.667 ns | 1.00 | 0.1779 | 1488 B | 1.00 | |
| 97 | +| ValueStringBuilder | 95.69 ns | 0.118 ns | 0.110 ns | 0.76 | 0.0669 | 560 B | 0.38 | |
| 98 | +</code></pre> |
| 99 | +<p>For more comparison check the documentation.</p> |
| 100 | +<p>Another benchmark shows that this <code>ValueStringBuilder</code> uses less memory when it comes to appending <code>ValueTypes</code> such as <code>int</code>, <code>double</code>, ...</p> |
| 101 | +<pre><code>| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated | |
| 102 | +|------------------------------- |---------:|--------:|--------:|-------:|-------:|----------:| |
| 103 | +| ValueStringBuilderAppendFormat | 821.7 ns | 1.29 ns | 1.14 ns | 0.4330 | - | 3.54 KB | |
| 104 | +| StringBuilderAppendFormat | 741.5 ns | 5.58 ns | 5.22 ns | 0.9909 | 0.0057 | 8.1 KB | |
| 105 | + |
| 106 | +</code></pre> |
| 107 | +<p>Checkout the <a href="https://github.com/linkdotnet/StringBuilder/tree/main/tests/LinkDotNet.StringBuilder.Benchmarks">Benchmark</a> for more detailed comparison and setup.</p> |
| 108 | +</article> |
| 109 | + </div> |
| 110 | + |
| 111 | + <div class="hidden-sm col-md-2" role="complementary"> |
| 112 | + <div class="sideaffix"> |
| 113 | + <div class="contribution"> |
| 114 | + <ul class="nav"> |
| 115 | + <li> |
| 116 | + <a href="https://github.com/linkdotnet/StringBuilder/blob/stable/docs/site/articles/comparison.md/#L1" class="contribution-link">Improve this Doc</a> |
| 117 | + </li> |
| 118 | + </ul> |
| 119 | + </div> |
| 120 | + <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix"> |
| 121 | + <h5>In This Article</h5> |
| 122 | + <div></div> |
| 123 | + </nav> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + |
| 130 | + <script type="text/javascript" src="../styles/docfx.vendor.js"></script> |
| 131 | + <script type="text/javascript" src="../styles/docfx.js"></script> |
| 132 | + <script type="text/javascript" src="../styles/main.js"></script> |
| 133 | + </body> |
| 134 | +</html> |
0 commit comments