Skip to content

Commit beb5879

Browse files
committed
[Count] add benchmark.
1 parent 454278e commit beb5879

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## ArrayWhereCount
2+
3+
### Source
4+
[ArrayWhereCount.cs](../../src/StructLinq.Benchmark/ArrayWhereCount.cs)
5+
6+
### Results:
7+
``` ini
8+
9+
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
10+
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
11+
.NET Core SDK=5.0.102
12+
[Host] : .NET Core 5.0.2 (CoreCLR 5.0.220.61120, CoreFX 5.0.220.61120), X64 RyuJIT
13+
DefaultJob : .NET Core 5.0.2 (CoreCLR 5.0.220.61120, CoreFX 5.0.220.61120), X64 RyuJIT
14+
15+
16+
```
17+
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
18+
|------------------------------- |----------:|----------:|----------:|------:|--------:|------:|------:|------:|----------:|
19+
| HandmadedCode | 9.018 μs | 0.1786 μs | 0.3442 μs | 1.00 | 0.00 | - | - | - | - |
20+
| SysLinq | 28.411 μs | 0.1726 μs | 0.1614 μs | 3.13 | 0.12 | - | - | - | 48 B |
21+
| StructLinqWithDelegate | 31.017 μs | 0.6127 μs | 1.5259 μs | 3.47 | 0.24 | - | - | - | 64 B |
22+
| StructLinqWithDelegateZeoAlloc | 30.736 μs | 0.6101 μs | 1.5195 μs | 3.42 | 0.25 | - | - | - | - |
23+
| StructLinqWithFunction | 16.965 μs | 0.3245 μs | 0.4104 μs | 1.87 | 0.09 | - | - | - | - |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Linq;
2+
using BenchmarkDotNet.Attributes;
3+
4+
namespace StructLinq.Benchmark
5+
{
6+
[MemoryDiagnoser]
7+
public class ArrayWhereCount
8+
{
9+
private const int Count = 10000;
10+
private readonly int[] array;
11+
12+
public ArrayWhereCount()
13+
{
14+
array = Enumerable.Range(0, Count).ToArray();
15+
}
16+
17+
[Benchmark(Baseline = true)]
18+
public int HandmadedCode()
19+
{
20+
int count = 0;
21+
for (int i = 0; i < array.Length; i++)
22+
{
23+
var elt = array[i];
24+
if ((elt & 1) == 0)
25+
{
26+
count++;
27+
}
28+
}
29+
return count;
30+
}
31+
32+
[Benchmark]
33+
public int SysLinq() => array
34+
.Where(x => (x & 1) == 0)
35+
.Count();
36+
37+
[Benchmark]
38+
public int StructLinqWithDelegate() => array.ToStructEnumerable()
39+
.Where(x => (x & 1) == 0)
40+
.Count();
41+
42+
[Benchmark]
43+
public int StructLinqWithDelegateZeoAlloc() => array.ToStructEnumerable()
44+
.Where(x => (x & 1) == 0, x=> x)
45+
.Count(x=>x);
46+
47+
[Benchmark]
48+
public int StructLinqWithFunction()
49+
{
50+
var where = new WherePredicate();
51+
return array
52+
.ToStructEnumerable()
53+
.Where(ref @where, x => x)
54+
.Count(x => x);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)