Advertisement

Basics operation and performances...

Started by March 14, 2002 04:59 PM
7 comments, last by V3rt3x 22 years, 11 months ago
Hi everybody! Is there somewhere an article about basic operations like sums, multiplications, ..., and how to improve performances (divide by i or multiply by 1/i ?) ? I whish someone has understood me
like bit shifting?
Advertisement
This two part article might be what you're looking for:
The Art of Optimising: http://www.cfxweb.net/article.php?sid=630

[edited by - Scarab0 on March 15, 2002 7:51:23 AM]
Dirk =[Scarab]= Gerrits
no, what I want to say is that for exemple, if I want I a function to divide 3 terms by a scalair, should I do:

void DivieByScalar( const float scalar ){      a /= scalar;      b /= scalar;      c /= scalar;} 


or:
void DivieByScalar( const float scalar ){      float s = 1/scalar      a *= s;      b *= s;      c *= s;} 


for the best performances ?
I would say the 2nd one. One divide and 3 mults.

Even better, why not write a little program to test the speed of both?

Time how long it takes to do a million of the first function, then a million of the second function.
There are two answers: ''try it and see'' and ''it depends''

By ''it depends'' I mean it varies between differen processors, platforms, and even compilers. Most modern processors perform multiplication faster than division, but how much by varies considerably, and in code like yours the overhead of loading the values into and out of processor registers may be more significant.

So the best answer is try it and see: write the code both ways and see what difference it makes, using a profiler or by adding your own timers to your program around the critical parts of the code. Do it within your program as other parts of the progam, such as what''s loaded into the cache before this code is executed, can have a significant effect.
John BlackburneProgrammer, The Pitbull Syndicate
Advertisement
One cool trick which you don''t see mentioned in many places is this:

Instead of

a = b / c
d = e / f

Use:

g = 1 / (c * f)

a = b * g * f ''(b / (c * f)) * f = b / c
d = e * g * c ''(d / (c * f)) * c = e / f

It works out to be faster, and the only loss is that you need an extra variable, readability and possibly a loss of prescision depending on the numbers. Oh yeah, and the same concept can be extended to do three or more divisions using only one divide and a couple of multiplies.

Trying is the first step towards failure.
Trying is the first step towards failure.
This is a point that''s always brought up in threads like this one, but I''ll re-iterate because it''s important.

Unless you''ve profiled your code and that function is high up on the list of "% time" - that is, you''re spending most of your time in that function - then don''t bother trying to optimize it. I''d bet if you profiled your code, that function wouldn''t even show up in the list.

There''s an old saying that you should always keep foremost in your mind when trying to optimize: "90% of your time is spent executing 10% of your code". Don''t bother trying to optimize that 90% of code which is hardly ever executed.


codeka.com - Just click it.
You should also have a performance goal. A little less absolute, but a good general rule, is that you should also address performance last. People can argue with it, but if you take a perspective of one person having the expertise to optimize and another doing the coding it is pretty clear when it should be done. With my job there were 35 programmers so you want them to finish the program. Once they finish it you want to know if it performs at a reasonable level. If it does then release it. No reason to dink around in it. If they don''t meet the goal then you start looking into why that is and the starting point is benchmarking/profiling.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement