Browse Source

Expand Clamp benchmark [skip ci]

pull/97/head
James Jackson-South 9 years ago
parent
commit
660eecf0f2
  1. 63
      tests/ImageSharp.Benchmarks/General/Clamp.cs

63
tests/ImageSharp.Benchmarks/General/Clamp.cs

@ -6,6 +6,7 @@
namespace ImageSharp.Benchmarks.General
{
using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
@ -17,24 +18,24 @@ namespace ImageSharp.Benchmarks.General
[Benchmark(Baseline = true, Description = "Maths Clamp")]
public byte ClampMaths()
{
int value = this.Value;
return (byte)Math.Min(Math.Max(0, value), 255);
int value = this.Value;
return (byte)Math.Min(Math.Max(byte.MinValue, value), byte.MaxValue);
}
[Benchmark(Description = "No Maths Clamp")]
public byte ClampNoMaths()
{
int value = this.Value;
value = value >= 255 ? 255 : value;
return (byte)(value <= 0 ? 0 : value);
int value = this.Value;
value = value >= byte.MaxValue ? byte.MaxValue : value;
return (byte)(value <= byte.MinValue ? byte.MinValue : value);
}
[Benchmark(Description = "No Maths No Equals Clamp")]
public byte ClampNoMathsNoEquals()
{
int value = this.Value;
value = value > 255 ? 255 : value;
return (byte)(value < 0 ? 0 : value);
int value = this.Value;
value = value > byte.MaxValue ? byte.MaxValue : value;
return (byte)(value < byte.MinValue ? byte.MinValue : value);
}
[Benchmark(Description = "No Maths Clamp No Ternary")]
@ -42,14 +43,14 @@ namespace ImageSharp.Benchmarks.General
{
int value = this.Value;
if (value >= 255)
if (value >= byte.MaxValue)
{
return 255;
return byte.MaxValue;
}
if (value <= 0)
if (value <= byte.MinValue)
{
return 0;
return byte.MinValue;
}
return (byte)value;
@ -58,19 +59,37 @@ namespace ImageSharp.Benchmarks.General
[Benchmark(Description = "No Maths No Equals Clamp No Ternary")]
public byte ClampNoMathsEqualsNoTernary()
{
int value = this.Value;
int value = this.Value;
if (value > 255)
{
return 255;
}
if (value > byte.MaxValue)
{
return byte.MaxValue;
}
if (value < 0)
{
return 0;
}
if (value < byte.MinValue)
{
return byte.MinValue;
}
return (byte)value;
return (byte)value;
}
[Benchmark(Description = "Clamp using Bitwise Abs")]
public byte ClampBitwise()
{
int x = this.Value;
int absmax = byte.MaxValue - x;
x = (x + byte.MaxValue - AbsBitwiseVer(ref absmax)) >> 1;
x = (x + byte.MinValue + AbsBitwiseVer(ref x)) >> 1;
return (byte)x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int AbsBitwiseVer(ref int x)
{
int y = x >> 31;
return (x ^ y) - y;
}
}
}

Loading…
Cancel
Save