-
Notifications
You must be signed in to change notification settings - Fork 24
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 #21 from thomaslevesque/perf-optim
Performance optimization of Batch, IndexOf and LastIndexOf
- Loading branch information
Showing
5 changed files
with
156 additions
and
17 deletions.
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
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,49 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Linq.Extras.Benchmarks | ||
{ | ||
public class BatchBenchmark | ||
{ | ||
private int[] _array; | ||
|
||
[Params(100, 1000, 10000)] | ||
public int N { get; set; } | ||
|
||
[Params(10, 30, 600)] | ||
public int BatchSize { get; set; } | ||
|
||
[GlobalSetup] | ||
public void Prepare() | ||
{ | ||
_array = Enumerable.Range(0, N).ToArray(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void RegularForLoop() | ||
{ | ||
for (int i = 0; i < _array.Length; i += BatchSize) | ||
{ | ||
int size = Math.Min(BatchSize, _array.Length - i); | ||
var batch = new int[size]; | ||
for (int j = 0; j < size; j++) | ||
{ | ||
batch[j] = _array[i + j]; | ||
} | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void UsingBatch() | ||
{ | ||
foreach (var batch in _array.Batch(BatchSize)) | ||
{ | ||
} | ||
} | ||
|
||
} | ||
} |
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,45 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Linq.Extras.Benchmarks | ||
{ | ||
public class IndexOfBenchmark | ||
{ | ||
private int[] _array; | ||
|
||
[Params(10, 500, 10000)] | ||
public int N { get; set; } | ||
|
||
private const int SearchedValue = 42; | ||
|
||
[GlobalSetup] | ||
public void Prepare() | ||
{ | ||
_array = Enumerable.Repeat(0, N).ToArray(); | ||
_array[N - 1] = SearchedValue; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public int RegularForLoop() | ||
{ | ||
int value = SearchedValue; | ||
for (int i = 0; i < N; i++) | ||
{ | ||
if (_array[i] == value) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
[Benchmark] | ||
public int UsingArrayIndexOf() => Array.IndexOf(_array, SearchedValue); | ||
|
||
[Benchmark] | ||
public int UsingIndexOf() => _array.IndexOf(SearchedValue); | ||
} | ||
} |
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