Last active
January 18, 2023 10:01
-
-
Save lukasreuter/12fbc01714924b5a75fd1a987eb85f5b to your computer and use it in GitHub Desktop.
Lingen Benchmarking (needs Performance Testing package)
This file contains hidden or 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
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Jobs; | |
namespace Tests | |
{ | |
[BurstCompile(CompileSynchronously = true, FloatMode = FloatMode.Fast, OptimizeFor = OptimizeFor.Performance, | |
DisableSafetyChecks = true)] | |
public struct MaxForLoopJob : IJob | |
{ | |
[ReadOnly] | |
public NativeArray<int> input; | |
[WriteOnly] | |
public NativeReference<int> output; | |
public void Execute() | |
{ | |
var max = int.MinValue; | |
var length = input.Length; | |
for (int i = 0; i < length; ++i) | |
{ | |
if (input[i] > max) | |
{ | |
max = input[i]; | |
} | |
} | |
output.Value = max; | |
} | |
} | |
} |
This file contains hidden or 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
using Cathei.LinqGen; | |
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Jobs; | |
namespace Tests | |
{ | |
[BurstCompile(CompileSynchronously = true, FloatMode = FloatMode.Fast, OptimizeFor = OptimizeFor.Performance, | |
DisableSafetyChecks = true)] | |
public struct MaxLinqGenJob : IJob | |
{ | |
[ReadOnly] | |
public NativeArray<int> input; | |
[WriteOnly] | |
public NativeReference<int> output; | |
public void Execute() | |
{ | |
var list = new NativeList<int>(); | |
list.Gen().Max(); | |
var fs = new FixedList128Bytes<int>(); | |
fs.Gen().Max(); | |
output.Value = input.Gen().Max(); | |
} | |
} | |
} |
This file contains hidden or 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
using System.Diagnostics.CodeAnalysis; | |
using NUnit.Framework; | |
using Tests; | |
using Unity.Collections; | |
using Unity.Jobs; | |
using Unity.PerformanceTesting; | |
// ReSharper disable once CheckNamespace | |
[SuppressMessage("ReSharper", "AccessToDisposedClosure")] | |
public class PerfComparison | |
{ | |
private readonly SampleGroup sampleGroup = new("µs", SampleUnit.Microsecond); | |
private const int ITERATIONS = 10; | |
private const int ARRAY_SIZE = 10000; | |
private const int WARMUP = 50; | |
private const int MEASURE = 10000; | |
[Test, Performance] | |
public void SquareAndSumLinqGen() | |
{ | |
using var input = GetTestData(Allocator.TempJob, ARRAY_SIZE); | |
using var output = new NativeReference<int>(Allocator.TempJob); | |
Measure.Method(() => | |
{ | |
new SquareAndSumLinqGenJob | |
{ | |
input = input, | |
output = output, | |
}.Run(); | |
}). | |
WarmupCount(WARMUP). | |
MeasurementCount(MEASURE). | |
IterationsPerMeasurement(ITERATIONS). | |
SampleGroup(sampleGroup). | |
Run(); | |
} | |
[Test, Performance] | |
public void SquareAndSumForLoop() | |
{ | |
using var input = GetTestData(Allocator.TempJob, ARRAY_SIZE); | |
using var output = new NativeReference<int>(Allocator.TempJob); | |
Measure.Method(() => | |
{ | |
new SquareAndSumForLoopJob | |
{ | |
input = input, | |
output = output, | |
}.Run(); | |
}). | |
WarmupCount(WARMUP). | |
MeasurementCount(MEASURE). | |
IterationsPerMeasurement(ITERATIONS). | |
SampleGroup(sampleGroup). | |
Run(); | |
} | |
[Test, Performance] | |
public void MaxForLoop() | |
{ | |
using var input = GetTestData(Allocator.TempJob, ARRAY_SIZE); | |
using var output = new NativeReference<int>(Allocator.TempJob); | |
Measure.Method(() => | |
{ | |
new MaxForLoopJob | |
{ | |
input = input, | |
output = output, | |
}.Run(); | |
}). | |
WarmupCount(WARMUP). | |
MeasurementCount(MEASURE). | |
IterationsPerMeasurement(ITERATIONS). | |
SampleGroup(sampleGroup). | |
Run(); | |
} | |
[Test, Performance] | |
public void MaxLinqGen() | |
{ | |
using var input = GetTestData(Allocator.TempJob, ARRAY_SIZE); | |
using var output = new NativeReference<int>(Allocator.TempJob); | |
Measure.Method(() => | |
{ | |
new MaxLinqGenJob | |
{ | |
input = input, | |
output = output, | |
}.Run(); | |
}). | |
WarmupCount(WARMUP). | |
MeasurementCount(MEASURE). | |
IterationsPerMeasurement(ITERATIONS). | |
SampleGroup(sampleGroup). | |
Run(); | |
} | |
private NativeArray<int> GetTestData(Allocator allocator, int count) | |
{ | |
var array = new NativeArray<int>(count, allocator); | |
for (int i = 0; i < count; i++) | |
{ | |
array[i] = i; | |
} | |
return array; | |
} | |
} |
This file contains hidden or 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
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Jobs; | |
namespace Tests | |
{ | |
[BurstCompile(CompileSynchronously = true, FloatMode = FloatMode.Fast, OptimizeFor = OptimizeFor.Performance, | |
DisableSafetyChecks = true)] | |
public struct SquareAndSumForLoopJob : IJob | |
{ | |
[ReadOnly] | |
public NativeArray<int> input; | |
[WriteOnly] | |
public NativeReference<int> output; | |
public void Execute() | |
{ | |
var sum = 0; | |
foreach (var i in input) | |
{ | |
sum += i * 10; | |
} | |
output.Value = sum; | |
} | |
} | |
} |
This file contains hidden or 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
using Cathei.LinqGen; | |
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Jobs; | |
namespace Tests | |
{ | |
[BurstCompile(CompileSynchronously = true, FloatMode = FloatMode.Fast, OptimizeFor = OptimizeFor.Performance, | |
DisableSafetyChecks = true)] | |
public struct SquareAndSumLinqGenJob : IJob | |
{ | |
[ReadOnly] | |
public NativeArray<int> input; | |
[WriteOnly] | |
public NativeReference<int> output; | |
public void Execute() | |
{ | |
output.Value = input.Gen().Select(new Selector()).Sum(); | |
} | |
} | |
public struct Selector : IStructFunction<int, int> | |
{ | |
public int Invoke(int arg) => arg * 10; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment