I am not sure if "clamping" is the correct terminology for this, however I really don't know what else to call it. Suppose we wanted to limit an integer to stay within some arbitrary range, like 0-50. This can be easily achieved by testing the current value with an if statement and assigning the maximum or minimum value accordingly. However, what is the fastest way to keep the Integer at its maximum value or minimum value?
Asked
Active
Viewed 7,655 times
2
-
http://stackoverflow.com/a/3176617/442444 – Carbine Jan 12 '15 at 04:38
-
If an inbuilt feature is what you are looking for clamping in C#, that might not be available. Its safe to use these kind of abstractions to serve your purpose without worrying about Performance bottleneck. – Carbine Jan 12 '15 at 04:42
-
Checking against a min and / or max is ultimately typically going to "compile" down to an integer compare and branch instruction on the CPU which is around a 5-10 clock cycles or less. Why do you care about clock cycles at this level. Surely, it's far more important to make your code readable and easier to maintain then using some fancy bit-bit shifting or boolean masking trickery that saves you 1 clock cycle. – Code Uniquely Jan 12 '15 at 04:45
-
@BrianRasmussen I was planning on writing a small wrapper class for min-max clamped integers. I was going to use operator overloading and I just wanted the fastest way possible. Better to write it correctly the first time then come back later and change it. – Krythic Jan 12 '15 at 04:45
-
@Krythic: in programming very few things have the "right" implementation. – zerkms Jan 12 '15 at 04:45
-
1Possible duplicate: already has been answered here: 'How to force a number to be in a range in C#?' (http://stackoverflow.com/questions/3176602/how-to-force-a-number-to-be-in-a-range-in-c). – Alexander Bell Jan 12 '15 at 04:49
-
1@krythic You asked for the fastest, not the "right" way :) – Code Uniquely Jan 12 '15 at 05:04
-
Does this answer your question? [Where can I find the "clamp" function in .NET?](https://stackoverflow.com/questions/2683442/where-can-i-find-the-clamp-function-in-net) – phuclv Apr 06 '22 at 10:56
3 Answers
5
As easy as
var normalized = Math.Min(50, Math.Max(0, value));
As of performance:
public static int Max(int val1, int val2) {
return (val1>=val2)?val1:val2;
}
That's how it's implemented in .NET, so it's unlikely you can implement it even better.

zerkms
- 249,484
- 69
- 436
- 539
-
And this is known to be the fastest? I was expecting some kind of fancy bit-shifting or something. – Krythic Jan 12 '15 at 04:39
-
5"Fastest" compared to what? There is no absolute "the best solution" for everything. – zerkms Jan 12 '15 at 04:40
-
It does look like others suggest this, too, given that another link showed the same thing but implemented slightly different. I will accept you as the answer. – Krythic Jan 12 '15 at 04:46
-
1
-
2
4
If you want speed, you should keep the CPU's branch predictor happy. So make the "happy path" return 'n' (the most probable outcome of calling 'Clamp'):
public static int Clamp( int n, int min, int max ) {
if( value < min ) return min;
if( value > max ) return max;
return n;
}

Elliott Prechter
- 91
- 3
0
Math.Clamp
was introduced since .NET Core 2.0 so you can use it directly. It should be the current fastest method
Math.Clamp(12, 0, 50)

phuclv
- 37,963
- 15
- 156
- 475