mirror of https://github.com/SixLabors/ImageSharp
12 changed files with 55 additions and 275 deletions
@ -1,77 +0,0 @@ |
|||||
namespace SixLabors.ImageSharp.Benchmarks |
|
||||
{ |
|
||||
using System.Numerics; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
using BenchmarkDotNet.Attributes; |
|
||||
|
|
||||
using SixLabors.ImageSharp; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Compares two implementation candidates for general BulkPixelOperations.ToVector4():
|
|
||||
/// - One iterating with pointers
|
|
||||
/// - One iterating with ref locals
|
|
||||
/// </summary>
|
|
||||
public unsafe class PackFromVector4ReferenceVsPointer |
|
||||
{ |
|
||||
private Buffer<Rgba32> destination; |
|
||||
|
|
||||
private Buffer<Vector4> source; |
|
||||
|
|
||||
[Params(16, 128, 1024)] |
|
||||
public int Count { get; set; } |
|
||||
|
|
||||
[GlobalSetup] |
|
||||
public void Setup() |
|
||||
{ |
|
||||
this.destination = Configuration.Default.MemoryManager.Allocate<Rgba32>(this.Count); |
|
||||
this.source = Configuration.Default.MemoryManager.Allocate<Vector4>(this.Count * 4); |
|
||||
this.source.Pin(); |
|
||||
this.destination.Pin(); |
|
||||
} |
|
||||
|
|
||||
[GlobalCleanup] |
|
||||
public void Cleanup() |
|
||||
{ |
|
||||
this.source.Dispose(); |
|
||||
this.destination.Dispose(); |
|
||||
} |
|
||||
|
|
||||
[Benchmark(Baseline = true)] |
|
||||
public void PackUsingPointers() |
|
||||
{ |
|
||||
Vector4* sp = (Vector4*)this.source.Pin(); |
|
||||
byte* dp = (byte*)this.destination.Pin(); |
|
||||
int count = this.Count; |
|
||||
int size = sizeof(Rgba32); |
|
||||
|
|
||||
for (int i = 0; i < count; i++) |
|
||||
{ |
|
||||
Vector4 v = Unsafe.Read<Vector4>(sp); |
|
||||
Rgba32 c = default(Rgba32); |
|
||||
c.PackFromVector4(v); |
|
||||
Unsafe.Write(dp, c); |
|
||||
|
|
||||
sp++; |
|
||||
dp += size; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public void PackUsingReferences() |
|
||||
{ |
|
||||
ref Vector4 sp = ref this.source.DangerousGetPinnableReference(); |
|
||||
ref Rgba32 dp = ref this.destination.DangerousGetPinnableReference(); |
|
||||
int count = this.Count; |
|
||||
|
|
||||
for (int i = 0; i < count; i++) |
|
||||
{ |
|
||||
dp.PackFromVector4(sp); |
|
||||
|
|
||||
sp = Unsafe.Add(ref sp, 1); |
|
||||
dp = Unsafe.Add(ref dp, 1); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,73 +0,0 @@ |
|||||
namespace SixLabors.ImageSharp.Benchmarks.General |
|
||||
{ |
|
||||
using System.Numerics; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
using BenchmarkDotNet.Attributes; |
|
||||
|
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
public class IterateArray |
|
||||
{ |
|
||||
// Usual pinned stuff
|
|
||||
private Buffer<Vector4> buffer; |
|
||||
|
|
||||
// An array that's not pinned by intent!
|
|
||||
private Vector4[] array; |
|
||||
|
|
||||
[Params(64, 1024)] |
|
||||
public int Length { get; set; } |
|
||||
|
|
||||
[GlobalSetup] |
|
||||
public void Setup() |
|
||||
{ |
|
||||
this.buffer = Configuration.Default.MemoryManager.Allocate<Vector4>(this.Length); |
|
||||
this.buffer.Pin(); |
|
||||
this.array = new Vector4[this.Length]; |
|
||||
} |
|
||||
|
|
||||
[Benchmark(Baseline = true)] |
|
||||
public Vector4 IterateIndexed() |
|
||||
{ |
|
||||
Vector4 sum = new Vector4(); |
|
||||
Vector4[] a = this.array; |
|
||||
|
|
||||
for (int i = 0; i < a.Length; i++) |
|
||||
{ |
|
||||
sum += a[i]; |
|
||||
} |
|
||||
return sum; |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public unsafe Vector4 IterateUsingPointers() |
|
||||
{ |
|
||||
Vector4 sum = new Vector4(); |
|
||||
|
|
||||
Vector4* ptr = (Vector4*) this.buffer.Pin(); |
|
||||
Vector4* end = ptr + this.Length; |
|
||||
|
|
||||
for (; ptr < end; ptr++) |
|
||||
{ |
|
||||
sum += *ptr; |
|
||||
} |
|
||||
|
|
||||
return sum; |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public Vector4 IterateUsingReferences() |
|
||||
{ |
|
||||
Vector4 sum = new Vector4(); |
|
||||
|
|
||||
ref Vector4 start = ref this.array[0]; |
|
||||
|
|
||||
for (int i = 0; i < this.Length; i++) |
|
||||
{ |
|
||||
sum += Unsafe.Add(ref start, i); |
|
||||
} |
|
||||
|
|
||||
return sum; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue