-
Notifications
You must be signed in to change notification settings - Fork 11
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
6 changed files
with
152 additions
and
1 deletion.
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,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; | ||
} | ||
|
||
|
||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |