Browse Source

Slightly faster clamp.

af/merge-core
James Jackson-South 10 years ago
parent
commit
166dafdf67
  1. 20
      src/ImageSharp/Common/Extensions/ComparableExtensions.cs
  2. 40
      tests/ImageSharp.Benchmarks/General/Clamp.cs

20
src/ImageSharp/Common/Extensions/ComparableExtensions.cs

@ -24,12 +24,12 @@ namespace ImageSharp
public static byte Clamp(this byte value, byte min, byte max) public static byte Clamp(this byte value, byte min, byte max)
{ {
// Order is important here as someone might set min to higher than max. // Order is important here as someone might set min to higher than max.
if (value > max) if (value >= max)
{ {
return max; return max;
} }
if (value < min) if (value <= min)
{ {
return min; return min;
} }
@ -48,12 +48,12 @@ namespace ImageSharp
/// </returns> /// </returns>
public static uint Clamp(this uint value, uint min, uint max) public static uint Clamp(this uint value, uint min, uint max)
{ {
if (value > max) if (value >= max)
{ {
return max; return max;
} }
if (value < min) if (value <= min)
{ {
return min; return min;
} }
@ -72,12 +72,12 @@ namespace ImageSharp
/// </returns> /// </returns>
public static int Clamp(this int value, int min, int max) public static int Clamp(this int value, int min, int max)
{ {
if (value > max) if (value >= max)
{ {
return max; return max;
} }
if (value < min) if (value <= min)
{ {
return min; return min;
} }
@ -96,12 +96,12 @@ namespace ImageSharp
/// </returns> /// </returns>
public static float Clamp(this float value, float min, float max) public static float Clamp(this float value, float min, float max)
{ {
if (value > max) if (value >= max)
{ {
return max; return max;
} }
if (value < min) if (value <= min)
{ {
return min; return min;
} }
@ -120,12 +120,12 @@ namespace ImageSharp
/// </returns> /// </returns>
public static double Clamp(this double value, double min, double max) public static double Clamp(this double value, double min, double max)
{ {
if (value > max) if (value >= max)
{ {
return max; return max;
} }
if (value < min) if (value <= min)
{ {
return min; return min;
} }

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

@ -11,26 +11,54 @@ namespace ImageSharp.Benchmarks.General
public class Clamp public class Clamp
{ {
[Params(-1, 0, 255, 256)]
public int Value { get; set; }
[Benchmark(Baseline = true, Description = "Maths Clamp")] [Benchmark(Baseline = true, Description = "Maths Clamp")]
public byte ClampMaths() public byte ClampMaths()
{ {
double value = 256; int value = this.Value;
return (byte)Math.Min(Math.Max(0, value), 255); return (byte)Math.Min(Math.Max(0, value), 255);
} }
[Benchmark(Description = "No Maths Clamp")] [Benchmark(Description = "No Maths Clamp")]
public byte ClampNoMaths() public byte ClampNoMaths()
{ {
double value = 256; int value = this.Value;
value = (value > 255) ? 255 : value; value = value >= 255 ? 255 : value;
value = (value < 0) ? 0 : value; return (byte)(value <= 0 ? 0 : value);
return (byte)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);
} }
[Benchmark(Description = "No Maths Clamp No Ternary")] [Benchmark(Description = "No Maths Clamp No Ternary")]
public byte ClampNoMathsNoTernary() public byte ClampNoMathsNoTernary()
{ {
double value = 256; int value = this.Value;
if (value >= 255)
{
return 255;
}
if (value <= 0)
{
return 0;
}
return (byte)value;
}
[Benchmark(Description = "No Maths No Equals Clamp No Ternary")]
public byte ClampNoMathsEqualsNoTernary()
{
int value = this.Value;
if (value > 255) if (value > 255)
{ {

Loading…
Cancel
Save