mirror of https://github.com/SixLabors/ImageSharp
8 changed files with 229 additions and 8 deletions
@ -0,0 +1,83 @@ |
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
using BenchmarkDotNet.Attributes; |
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Benchmarks.General.PixelConversion |
|||
{ |
|||
public class PixelConversion_ConvertToVector4 |
|||
{ |
|||
struct ConversionRunner<T> |
|||
where T : struct, ITestPixel<T> |
|||
{ |
|||
private T[] source; |
|||
|
|||
private Vector4[] dest; |
|||
|
|||
public ConversionRunner(int count) |
|||
{ |
|||
this.source = new T[count]; |
|||
this.dest = new Vector4[count]; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public void RunRetvalConversion() |
|||
{ |
|||
int count = this.source.Length; |
|||
|
|||
ref T sourceBaseRef = ref this.source[0]; |
|||
ref Vector4 destBaseRef = ref this.dest[0]; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
Unsafe.Add(ref destBaseRef, i) = Unsafe.Add(ref sourceBaseRef, i).ToVector4(); |
|||
} |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public void RunCopyToConversion() |
|||
{ |
|||
int count = this.source.Length; |
|||
|
|||
ref T sourceBaseRef = ref this.source[0]; |
|||
ref Vector4 destBaseRef = ref this.dest[0]; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
Unsafe.Add(ref sourceBaseRef, i).CopyToVector4(ref Unsafe.Add(ref destBaseRef, i)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private ConversionRunner<TestRgba> runner; |
|||
|
|||
[Params(32)] |
|||
public int Count { get; set; } |
|||
|
|||
[GlobalSetup] |
|||
public void Setup() |
|||
{ |
|||
this.runner = new ConversionRunner<TestRgba>(this.Count); |
|||
} |
|||
|
|||
[Benchmark(Baseline = true)] |
|||
public void UseRetval() |
|||
{ |
|||
this.runner.RunRetvalConversion(); |
|||
} |
|||
|
|||
[Benchmark] |
|||
public void UseCopyTo() |
|||
{ |
|||
this.runner.RunCopyToConversion(); |
|||
} |
|||
|
|||
// RESULTS:
|
|||
// Method | Count | Mean | Error | StdDev | Scaled |
|
|||
// ---------- |------ |---------:|----------:|----------:|-------:|
|
|||
// UseRetval | 32 | 94.99 ns | 1.1199 ns | 0.9352 ns | 1.00 |
|
|||
// UseCopyTo | 32 | 59.47 ns | 0.6104 ns | 0.5710 ns | 0.63 |
|
|||
} |
|||
} |
|||
@ -0,0 +1,95 @@ |
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
using BenchmarkDotNet.Attributes; |
|||
|
|||
namespace SixLabors.ImageSharp.Benchmarks.General.PixelConversion |
|||
{ |
|||
public class PixelConversion_ConvertToVector4_AsPartOfCompositeOperation |
|||
{ |
|||
struct ConversionRunner<T> |
|||
where T : struct, ITestPixel<T> |
|||
{ |
|||
private T[] source; |
|||
|
|||
private Vector4[] dest; |
|||
|
|||
public ConversionRunner(int count) |
|||
{ |
|||
this.source = new T[count]; |
|||
this.dest = new Vector4[count]; |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public void RunRetvalConversion() |
|||
{ |
|||
int count = this.source.Length; |
|||
|
|||
ref T sourceBaseRef = ref this.source[0]; |
|||
ref Vector4 destBaseRef = ref this.dest[0]; |
|||
|
|||
Vector4 temp; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
temp = Unsafe.Add(ref sourceBaseRef, i).ToVector4(); |
|||
|
|||
// manipulate pixel before saving to dest buffer:
|
|||
temp.W = 0; |
|||
|
|||
Unsafe.Add(ref destBaseRef, i) = temp; |
|||
} |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public void RunCopyToConversion() |
|||
{ |
|||
int count = this.source.Length; |
|||
|
|||
ref T sourceBaseRef = ref this.source[0]; |
|||
ref Vector4 destBaseRef = ref this.dest[0]; |
|||
|
|||
Vector4 temp = default; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
Unsafe.Add(ref sourceBaseRef, i).CopyToVector4(ref temp); |
|||
|
|||
// manipulate pixel before saving to dest buffer:
|
|||
temp.W = 0; |
|||
|
|||
Unsafe.Add(ref destBaseRef, i) = temp; |
|||
} |
|||
} |
|||
} |
|||
|
|||
private ConversionRunner<TestRgba> runner; |
|||
|
|||
[Params(32)] |
|||
public int Count { get; set; } |
|||
|
|||
[GlobalSetup] |
|||
public void Setup() |
|||
{ |
|||
this.runner = new ConversionRunner<TestRgba>(this.Count); |
|||
} |
|||
|
|||
[Benchmark(Baseline = true)] |
|||
public void UseRetval() |
|||
{ |
|||
this.runner.RunRetvalConversion(); |
|||
} |
|||
|
|||
[Benchmark] |
|||
public void UseCopyTo() |
|||
{ |
|||
this.runner.RunCopyToConversion(); |
|||
} |
|||
|
|||
// RESULTS:
|
|||
// Method | Count | Mean | Error | StdDev | Scaled |
|
|||
// ---------- |------ |----------:|----------:|----------:|-------:|
|
|||
// UseRetval | 32 | 100.35 ns | 0.4844 ns | 0.4532 ns | 1.00 |
|
|||
// UseCopyTo | 32 | 53.95 ns | 0.1269 ns | 0.1125 ns | 0.54 |
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue