-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from maxisoft/Span
Span based datastructures
- Loading branch information
Showing
14 changed files
with
1,993 additions
and
2 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Maxisoft.Utils.Benchmarks/Collections/Spans/BitSpanBenchmarks.cs
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,76 @@ | ||
using System; | ||
using System.Collections; | ||
using BenchmarkDotNet.Attributes; | ||
using Maxisoft.Utils.Collections.Lists; | ||
using Maxisoft.Utils.Collections.Spans; | ||
using Troschuetz.Random; | ||
|
||
namespace Maxisoft.Utils.Benchmarks.Collections.Spans | ||
{ | ||
[MemoryDiagnoser] | ||
public class BitSpanBenchmarks | ||
{ | ||
private const int repeat = 32; | ||
private ArrayList<int> Data = new ArrayList<int>(); | ||
private readonly TRandom _random = new TRandom(); | ||
|
||
[Params(1, 10, 100, 1000, 10_000)] public int N; | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
Data.Resize(N); | ||
for (var i = 0; i < N; i++) | ||
{ | ||
Data[i] = _random.Next(); | ||
} | ||
} | ||
|
||
[IterationCleanup] | ||
public void Cleanup() | ||
{ | ||
Data = new ArrayList<int>(); | ||
} | ||
|
||
[Benchmark] | ||
public void BitArray() | ||
{ | ||
var ba = new BitArray(Data.Data()); | ||
for (var i = 0; i < repeat; i++) | ||
{ | ||
ba.And(ba); | ||
ba.Or(ba); | ||
ba.Xor(ba); | ||
ba.SetAll(i % 2 == 0); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void BitSpan() | ||
{ | ||
BitSpan ba = Data.AsSpan(); | ||
for (var i = 0; i < repeat; i++) | ||
{ | ||
ba.And(ba); | ||
ba.Or(ba); | ||
ba.Xor(ba); | ||
ba.SetAll(i % 2 == 0); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void BitSpanWithCopy() | ||
{ | ||
Span<int> data = stackalloc int[10_000]; | ||
Data.AsSpan().CopyTo(data); | ||
BitSpan ba = data.Slice(0, N); | ||
for (var i = 0; i < repeat; i++) | ||
{ | ||
ba.And(ba); | ||
ba.Or(ba); | ||
ba.Xor(ba); | ||
ba.SetAll(i % 2 == 0); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.