Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

condition vs division

given the following statement which is executed a lot:

iNormVal = iVal / uRatio;

would the following make more sense (performance wise) if uRatio == 1 most (90%) of the time?

if(uRatio > 1)
iNormVal = iVal / uRatio;
else
iNormVal = iVal;

thanks..

like image 455
nantonop Avatar asked Jan 13 '12 09:01

nantonop


1 Answers

Since you spotted this as a potential bottleneck, it's very likely this spot is totally irrelevant for your app's overall speed. Seriously, humans, even guru programmers, are notoriously bad at spotting real bottlenecks. (The difference is that good programmers admit and preach that, while juniors keep spending time for optimizing irrelevant spots.)

Generally I found this approach to optimizations most helpful:

  1. If speed is a major concern, schedule considerable time for optimizations to be done before releasing your app.
  2. Design your code so that it doesn't sport inherent pessimizations.
  3. Implement it the way it's easiest to understand the code. Prevent obvious pessimizations (like passing parameters by value instead of reference), but don't get overexcited.
  4. Check if it is too slow. If so profile the app and identify the hot spots.
  5. Put resources into optimizing (and thereby potentially obfuscating) those hot spots only, iteratively profiling to check which changes help.
  6. Stop when the app is fast enough.

(It's different for library code, obviously, but these few steps would carry you a long way.)

like image 125
sbi Avatar answered Sep 18 '22 18:09

sbi