Summary: Our software uses a utility function, this function is called million times so we have to optimized it. I notice that with a simple tweak our code in this utility function, the execution is much faster, down from 10 seconds of execution to 0.6 seconds. Here's the tweak in the utility function:
From code1 (finished in 10 seconds):
double d = (a * x3 + b * y3 + c) / l;
if (std::abs(d) > inMaxDToL) return false;
d = (a * x4 + b * y4 + c) / l;
if (std::abs(d) > inMaxDToL) return false;
return false;
To code 2 (finised in 0.6 seconds):
double d = (a * x3 + b * y3 + c) / l;
if (std::abs(d) > inMaxDToL) return false;
return false;
What I did was returning false earlier in code 2. Obviously in the first code, line 1 + 2 is equivalent to line 3 + 4 in term of workload. So I wonder why removing line 3 + 4 can speed up the processing so much?
Another tweak that also reduce the execution time from 10s to 0.6 seconds is to replace an incall function call:
if (!inBetween(x1, y1, x2, y2, x3, y3)) return false;
with its content:
if ((x2 - x1) * (x2 - x3) > epsilon) return false;
if ((y2 - y1) * (y2 - y3) > epsilon) return false;
The code was also speed up from 10s to 0.6ms.
I'm using Visual Studio 2013 and its compiler.
Did I miss something here?
Edit: to make it clearer:
- That few lines are just a part of the utility function
- I purposedly return false for debugging purpose