Skip to content

Commit

Permalink
Merge pull request #13 from maxisoft/Span
Browse files Browse the repository at this point in the history
Span based datastructures
  • Loading branch information
maxisoft authored May 23, 2022
2 parents 3075d53 + 7f0dd04 commit 2a9b8aa
Show file tree
Hide file tree
Showing 14 changed files with 1,993 additions and 2 deletions.
76 changes: 76 additions & 0 deletions Maxisoft.Utils.Benchmarks/Collections/Spans/BitSpanBenchmarks.cs
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);
}
}
}
}
Loading

0 comments on commit 2a9b8aa

Please sign in to comment.