-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
namespace Advanced.Samples.Performance; | ||
|
||
|
||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Running; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
[MemoryDiagnoser(true)] | ||
public class Benchmarks | ||
{ | ||
private const long num = long.MaxValue; | ||
|
||
[Benchmark] | ||
public void GetDigitsFromLeastSignificant_String() | ||
{ | ||
num | ||
.ToString() | ||
.Select(x => Convert.ToByte(x.ToString())) | ||
.ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public void GetDigitsFromLeastSignificant_MathWithSpan() | ||
{ | ||
var result = new byte[20]; | ||
var span = new Span<byte>(result); | ||
var n = num; | ||
var index = 0; | ||
while (n > 0) | ||
{ | ||
span[index] = (byte)(n % 10); | ||
n /= 10; | ||
index++; | ||
} | ||
|
||
var res = span[..index].ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public void GetDigitsFromLeastSignificant_MathWithList() | ||
{ | ||
var result = new List<byte>(); | ||
var n = num; | ||
while (n > 0) | ||
{ | ||
result.Add((byte)(n % 10)); | ||
n /= 10; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void GetDigitsFromLeastSignificant_MathWithYield() | ||
{ | ||
IEnumerable<byte> Inner() | ||
{ | ||
var n = num; | ||
while (n > 0) | ||
{ | ||
yield return (byte)(n % 10); | ||
n /= 10; | ||
} | ||
} | ||
|
||
Inner().ToArray(); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
[Explicit] | ||
public class BenchmarkTests | ||
{ | ||
[Test] | ||
public void GetDigitsFromLeastSignificant() | ||
{ | ||
var config = ManualConfig | ||
.CreateMinimumViable() | ||
.WithOption(ConfigOptions.DisableOptimizationsValidator, true); | ||
|
||
BenchmarkRunner.Run<Benchmarks>(config); | ||
} | ||
} |