Skip to content

Commit

Permalink
Deploying to gh-pages from @ 4e15f3d 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource committed Jan 8, 2025
1 parent e92661d commit 5963bbf
Show file tree
Hide file tree
Showing 10 changed files with 706 additions and 23 deletions.
117 changes: 115 additions & 2 deletions rules/S1155/csharp-description.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ <h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_how_to_fix_it">How to fix it</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Prefer using <code>Any()</code> to test for emptiness over <code>Count()</code>.</p>
</div>
<div class="sect2">
<h3 id="_code_examples">Code examples</h3>
<div class="sect3">
<h4 id="_noncompliant_code_example">Noncompliant code example</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-csharp" data-lang="csharp">private static bool HasContent(IEnumerable&lt;string&gt; strings)
Expand All @@ -38,9 +50,9 @@ <h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Prefer using <code>Any()</code> to test for emptiness over <code>Count()</code>.</p>
</div>
<div class="sect3">
<h4 id="_compliant_solution">Compliant solution</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-csharp" data-lang="csharp">private static bool HasContent(IEnumerable&lt;string&gt; strings)
Expand All @@ -59,9 +71,110 @@ <h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_resources">Resources</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_benchmarks">Benchmarks</h3>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Method</th>
<th class="tableblock halign-left valign-top">Runtime</th>
<th class="tableblock halign-left valign-top">Mean</th>
<th class="tableblock halign-left valign-top">Standard Deviation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Count</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET 9.0</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">2,841.003 ns</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">266.0238 ns</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Any</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET 9.0</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1.749 ns</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0.1242 ns</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Count</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET Framework 4.8.1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">71,125.275 ns</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">731.0382 ns</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Any</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET Framework 4.8.1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">31.774 ns</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0.3196 ns</p></td>
</tr>
</tbody>
</table>
<div class="sect3">
<h4 id="_glossary">Glossary</h4>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/Arithmetic_mean">Mean</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Standard_deviation">Standard Deviation</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The results were generated by running the following snippet with <a href="https://github.com/dotnet/BenchmarkDotNet">BenchmarkDotNet</a>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-csharp" data-lang="csharp">private IEnumerable&lt;int&gt; collection;

public const int N = 10_000;

[GlobalSetup]
public void GlobalSetup()
{
collection = Enumerable.Range(0, N).Select(x =&gt; N - x);
}

[Benchmark(Baseline = true)]
public bool Count() =&gt;
collection.Count() &gt; 0;

[Benchmark]
public bool Any() =&gt;
collection.Any();</code></pre>
</div>
</div>
<div class="paragraph">
<p>Hardware Configuration:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5247/22H2/2022Update)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
[Host] : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET Framework 4.8.1 : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256</code></pre>
</div>
</div>
<hr>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation_specification">Implementation Specification</h2>
<div class="sectionbody">
Expand Down
140 changes: 136 additions & 4 deletions rules/S1643/csharp-description.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ <h2 id="_description">Description</h2>
<h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>StringBuilder</code> is more efficient than string concatenation, especially when the operator is repeated over and over as in loops.</p>
<p>Concatenating multiple string literals or strings using the <code>+</code> operator creates a new string object for each concatenation. This can lead to a large number of intermediate string objects and can be inefficient. The <code>StringBuilder</code> class is more efficient than string concatenation, especially when the operator is repeated over and over as in loops.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_how_to_fix_it">How to fix it</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Replace string concatenation with <code>StringBuilder</code>.</p>
</div>
<div class="sect2">
<h3 id="_noncompliant_code_example">Noncompliant code example</h3>
<h3 id="_code_examples">Code examples</h3>
<div class="sect3">
<h4 id="_noncompliant_code_example">Noncompliant code example</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-csharp" data-lang="csharp">string str = "";
Expand All @@ -22,8 +32,8 @@ <h3 id="_noncompliant_code_example">Noncompliant code example</h3>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_compliant_solution">Compliant solution</h3>
<div class="sect3">
<h4 id="_compliant_solution">Compliant solution</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-csharp" data-lang="csharp">StringBuilder bld = new StringBuilder();
Expand All @@ -34,10 +44,132 @@ <h3 id="_compliant_solution">Compliant solution</h3>
string str = bld.ToString();</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_resources">Resources</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_documentation">Documentation</h3>
<div class="ulist">
<ul>
<li>
<p>Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder">StringBuilder Class</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="_benchmarks">Benchmarks</h3>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Method</th>
<th class="tableblock halign-left valign-top">Runtime</th>
<th class="tableblock halign-left valign-top">Mean</th>
<th class="tableblock halign-left valign-top">Standard Deviation</th>
<th class="tableblock halign-left valign-top">Allocated</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">StringConcatenation</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET 9.0</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">45,723.79 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4,589.713 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">586280.56 KB</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">StringBuilder</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET 9.0</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">77.73 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1.221 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">243.79 KB</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">StringConcatenation</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET Framework 4.6.2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">33,922.61 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">302.498 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">586450.35 KB</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">StringBuilder</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">.NET Framework 4.6.2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">178.14 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">9.010 us</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">244.15 KB</p></td>
</tr>
</tbody>
</table>
<div class="sect3">
<h4 id="_glossary">Glossary</h4>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/Arithmetic_mean">Mean</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Standard_deviation">Standard Deviation</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The results were generated by running the following snippet with <a href="https://github.com/dotnet/BenchmarkDotNet">BenchmarkDotNet</a>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-csharp" data-lang="csharp">[Params(10_000)]
public int Iterations;

[Benchmark]
public void StringConcatenation()
{
string str = "";
for (int i = 0; i &lt; Iterations; i++)
{
str = str + "append";
}
}

[Benchmark]
public void StringBuilder()
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i &lt; Iterations; i++)
{
builder.Append("append");
}
_ = builder.ToString();
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Hardware Configuration:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5247/22H2/2022Update)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
[Host] : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET Framework 4.6.2 : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256</code></pre>
</div>
</div>
<hr>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation_specification">Implementation Specification</h2>
<div class="sectionbody">
Expand Down
Loading

0 comments on commit 5963bbf

Please sign in to comment.