turbo_laser
turbo_laser

Reputation: 261

Performance difference between comparing to an expression or a variable

I'm wondering about the difference in performance, if any, between handling control flow with an expression and assigning that expression to a variable and comparing to that instead. For example, what would be the difference in performance between:

double testVal = pow((point.x-center.x), 2.0) + (pow((point.y-center.y), 2.0));
double radSqr = pow(radius, 2.0);
if(testVal < radSqr) {
....................
} else if(testVal == radSqr) { 
..................
}

and

if(pow((point.x-center.x), 2.0) + (pow((point.y-center.y), 2.0)) < pow(radius, 2.0)) {
....................
} else if(testVal == radSqr) { 
..................
}

Upvotes: 0

Views: 53

Answers (1)

Jonas B&#246;tel
Jonas B&#246;tel

Reputation: 4492

You are asking the wrong question and here is a good and fun-to-read article why: http://ericlippert.com/2012/12/17/performance-rant/

If you really would have to know the answer measuring is you friend if done right: http://tech.pro/blog/1293/c-performance-benchmark-mistakes-part-one

Upvotes: 1

Related Questions