int X = a-b;
int d = Math.Abs(X);
I am pretty sure that .NET doesn’t do inlining. So, will I do if(), or is there some other less-known trick?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The JIT performs inlining in some circumstances. I don’t know whether it inlines
Math.Absor not… but have you verified that this is actually a performance problem for you? Don’t micro-optimize until you know that you need to, and then measure the performance gain from something like:to verify that it’s really worth it.
As noted by Anthony, the above won’t (normally) work for
int.MinValue, as-int.MinValue == int.MinValue, whereasMath.Abswill throw anOverflowException. You can force this in the straight C# as well using checked arithmetic: