The question pretty much says it all. At the moment, I'm using Math.Abs(a - b)
to calculate the absolute value of a subtraction expression, example 5 - 10
and 10 - 5
both returning 5
.
Is there a more efficient way to do this, or it is the most efficient way?
Please do not micro optimize your code until you know exactly where the problem is.
You can implement one your self and make it an inline function to reduce the cost of function call by using MethodImplAttribute.
If you are using Int32 Values and your inputs are not happen to be boundary values then you can use this
but I suggest you to forget all about it, profile your application, you will see the real problem is somewhere else not in calling
Math.Abs
, You should always make your code readable first then tweak performance problems after you prove that they are really performance problems!read this please