Skip to content

Commit

Permalink
[Repeat] add Repeat generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
reegeek committed Jun 30, 2020
1 parent 8acf239 commit d2be77a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/StructLinq.Benchmark/RangeWhereSelectSum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class RangeWhereSelectSum
public RangeWhereSelectSum()
{
}

[Benchmark(Baseline = true)]
public int SysSum()
{
Expand All @@ -41,6 +42,7 @@ public int SysSum()
}
return sum;
}

[Benchmark]
public int SysRangeWhereSelectSum() => Enumerable.Range(0, Count)
.Where(x=> (x & 1)==0)
Expand Down Expand Up @@ -81,5 +83,4 @@ public readonly int Eval(int element)
return element * 2;
}
}

}
54 changes: 54 additions & 0 deletions src/StructLinq.Benchmark/Repeat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Linq;
using BenchmarkDotNet.Attributes;

namespace StructLinq.Benchmark
{

//``` ini

//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
//Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
//.NET Core SDK=3.1.301
//[Host] : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT
//DefaultJob : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT


//```
//| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
//|----------------------- |----------:|----------:|----------:|------:|------:|------:|------:|----------:|
//| EnumerableRepeat | 38.829 us | 0.3285 us | 0.2912 us | 1.00 | - | - | - | 32 B |
//| StructEnumerableRepeat | 2.883 us | 0.0074 us | 0.0066 us | 0.07 | - | - | - | - |

[MemoryDiagnoser]
public class Repeat
{
private const int Count = 10000;
private const int value = 12;

[Benchmark(Baseline = true)]
public int EnumerableRepeat()
{
int sum = 0;
foreach (var result in Enumerable.Repeat(value, Count))
{
sum += result;
}

return sum;
}

[Benchmark]
public int StructEnumerableRepeat()
{
int sum = 0;
foreach (var result in StructEnumerable.Repeat(value, Count))
{
sum += result;
}

return sum;
}


}
}
26 changes: 26 additions & 0 deletions src/StructLinq.Tests/RepeatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Linq;
using FluentAssertions;
using StructLinq.Repeat;
using Xunit;

namespace StructLinq.Tests
{
public class RepeatTests : AbstractEnumerableTests<int, RepeatEnumerable<int>, RepeatEnumerator<int>>
{
protected override RepeatEnumerable<int> Build(int size)
{
return StructEnumerable.Repeat(-1, (uint)size);
}

[Theory]
[InlineData(0, 10)]
[InlineData(-10, 20)]
[InlineData(-20, 10)]
public void ShouldBeEqualToSystem(int element, int count)
{
var system = Enumerable.Repeat(element, count);
var structEnum = StructEnumerable.Repeat(element, (uint)count).ToEnumerable();
structEnum.Should().Equal(system);
}
}
}
24 changes: 24 additions & 0 deletions src/StructLinq/Repeat/RepeatEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StructLinq.Repeat
{
public readonly struct RepeatEnumerable<T> : IStructEnumerable<T, RepeatEnumerator<T>>
{
private readonly T element;
private readonly uint count;

public RepeatEnumerable(T element, uint count)
{
this.element = element;
this.count = count;
}

public RepeatEnumerator<T> GetEnumerator()
{
return new RepeatEnumerator<T>(element, count);
}
}
}
31 changes: 31 additions & 0 deletions src/StructLinq/Repeat/RepeatEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace StructLinq.Repeat
{
public struct RepeatEnumerator<T> : IStructEnumerator<T>
{
private readonly T element;
private readonly uint count;
private uint index;
public RepeatEnumerator(T element, uint count)
{
this.element = element;
this.count = count;
index = 0;
}

public void Dispose()
{
}

public bool MoveNext()
{
return index++ < count;
}

public void Reset()
{
index = 0;
}

public T Current => element;
}
}
15 changes: 15 additions & 0 deletions src/StructLinq/Repeat/StructEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.CompilerServices;
using StructLinq.Repeat;

// ReSharper disable once CheckNamespace
namespace StructLinq
{
public static partial class StructEnumerable
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RepeatEnumerable<T> Repeat<T>(T element, uint count)
{
return new RepeatEnumerable<T>(element, count);
}
}
}

0 comments on commit d2be77a

Please sign in to comment.