Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Performance: Division & Multiplication

Credits go to aj5555 and Thomas Harte for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Performance: Division & Multiplication
type568
Member #8,381
March 2007
avatar

I heard, that division (/) is a very slow operator, and that * is a lot faster. So, my question is, are these rumors true, for floating point variables(float)? I have some performance sensitive code, where objects have their masses, and their accelerations vary according to them. I have to divide, the force affecting an object, by the object's mass, to get the acceleration. Should I store 1/mass, and then multiply by the mass, to gain performance?

Thanks.

Thomas Harte
Member #33
April 2000
avatar

It's not really that simple any more. Most processors have multiple arithmetic units, and they schedule your instructions to try and use as many of those as possible at once. Nowadays most arithmetic units that can do both multiply and divide do them in the same amount of time, but some CPUs have units that can't do both. As an example, the G5 which Apple used prior to switching to Intel had two arithmetic units, one could do divide and multiply, the other only multiply. The processor before, the G4, had four arithmetic units, of which three could only do addition and subtraction and the fourth could do divide and multiply.

To be honest, I don't think storing 1/mass is going to make any tangible difference to your processing. But you might consider storing 1/mass anyway, because it means you can conceptually store objects with 'infinite' mass, i.e. objects that cannot be moved. Which are quite common in games.

type568
Member #8,381
March 2007
avatar

Thank you, and the idea of 1/mass, is great, I planned to hold another integer variable, "is_static" for this purpose.

But, after looking at the code, and thinking about the division, I found out that, I also have a variable similar to fps (frames per second..) by which I divide a lot of things every frame, to guarantee the picture motion not to be fps dependent. So, should I replace it with 1/(it)?

Thanks.

Goalie Ca
Member #2,579
July 2002
avatar

if you are going to be dividing by 1/mass in a lot of places it is a really good idea to cache 1/mass and multiply instead.

-------------
Bah weep granah weep nini bong!

type568
Member #8,381
March 2007
avatar

Quote:

if you are going to be dividing by 1/mass in a lot of places it is a really good idea to cache 1/mass and multiply instead.

I'm going to get different results then, I suppose..

aj5555
Member #9,033
September 2007

1. premature optimzation. your sensitive code probably amounts to .001% of your programs runtime. dont get your panties in a bunch over code that takes .001% of runtime.

2. division is slower than multiply. sometimes (and its worth timing this to be sure).. you can multiply by a factor, then >> to achieve the same division, but at higher speed. it really depends on how many items you have to divide.

3. trust no one. Benchmark your results, half of everything you hear is rumour or based on limitations of old hardware.

type568
Member #8,381
March 2007
avatar

I'm doing a "model", of some physical phenomenon, so actually the performance limits the amount of objects can be processed at a time, and the complexity is n*m.

Thanks.

Audric
Member #907
January 2001

There are compiler options to compile in profile mode. It will add extra instructions in the executable that count how many time each function is used, how much time is spent inside, etc. After a good run, you can analyze the report to see which functions are worth your time.

Evert
Member #794
November 2000
avatar

Quote:

So, my question is, are these rumors true, for floating point variables(float)?

Oh, yes.

Quote:

Should I store 1/mass,

Yes, definately. Avoid divisions as much as you can.

Quote:

and then multiply by the mass

Why do you need to multiply by the mass again? If you're dividing it out first and then multiply by it again, the best thing to do is not to do either. ;)

Albin Engström
Member #8,110
December 2006
avatar

Um, isn't multiplication faster because you don't always deal with floats, i mean, would this 100*0.5 be faster than 100/2? ???

Audric
Member #907
January 2001

AFAIK, converting int <-> float is slower than any mathematical operator.

GullRaDriel
Member #3,861
September 2003
avatar

I remember using bit shift for power of two multiplication/division on integers.

int a = 200 , b = 50 , result = 0 ;

result = ( a >> 1 ) + ( b << 1 ) ;
/* result = ( a / 2)  + ( b * 2 ) */

printf( "%d\n" , result ); /* this would output 200 */

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Evert
Member #794
November 2000
avatar

Guys, the original question was about floats specifically.

For integers, I think the difference between multiplication and division (in terms of speed) is less severe, if I recall correctly. Yes, bitshifts are faster for integer power-of-two divisions (or multiplications for that matter), but the compiler should catch those anyway and do the optimisation for you.

Oh, minor detail: don't mix float and integer arithmetic if you can avoid it. It'll be slower than plain floats becaused you get the extra cost of promoting the integer to a float.

Audric
Member #907
January 2001

By the way, in the C sources, I'm not sure the compiler optimizes implicit conversions on litterals, so be sure to write float litterals : 1.0 / mass, not 1 / mass.

type568
Member #8,381
March 2007
avatar

Quote:

Why do you need to multiply by the mass again? If you're dividing it out first and then multiply by it again, the best thing to do is not to do either. ;)

Well, I mean multiply by the coefficient that I get..

Quote:

Audric: By the way, in the C sources, I'm not sure the compiler optimizes implicit conversions on litterals, so be sure to write float litterals : 1.0 / mass, not 1 / mass.

Thanks, but I'm quite sure that if mass, is a float- in both cases I'm going to get float as a result.

And, yes I'm using only floats..

And, by the way, your posts have awakened another question:

A: 100/2
B: 100*0.5

What is faster?

Quote:

Audric: There are compiler options to compile in profile mode. It will add extra instructions in the executable that count how many time each function is used, how much time is spent inside, etc. After a good run, you can analyze the report to see which functions are worth your time.

I have no idea how to enable it in VS8 though :( What is this option's name?

Thanks, to everybody.

Audric
Member #907
January 2001

Quote:

if mass is a float, in both cases I'm going to get float as a result.

You don't seem to believe my advice that unnecessary casting ints to floats will make your "optimization" slower than the original.

Quote:

A: 100/2
B: 100*0.5
What is faster?

For a C compiler, A produces an integer, while B takes 200% time and produces a float.

Profiling: No idea, the Allegro manual only gives precise indications for gcc compilers.

Vanneto
Member #8,643
May 2007

Quote:

You don't seem to believe my advice that unnecessary casting ints to floats will make your "optimization" slower than the original.

Your advice is valid and true. But wouldn't the compiler cast the int to a float at compile-time?

In capitalist America bank robs you.

type568
Member #8,381
March 2007
avatar

Quote:

You don't seem to believe my advice that unnecessary casting ints to floats will make your "optimization" slower than the original.

Oh, ok.. But as aj5555 said, only what worth optimization should be optimized, and mass / mass_coefficient, are only initialized once.

HoHo
Member #4,534
April 2004
avatar

Quote:

I have no idea how to enable it in VS8 though :(

There is no such mode in the free version. AFAIK it is only availiable in the enterprise version. Get a real compiler, like GCC ;)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Audric
Member #907
January 2001

If you put integer litterals in float computations, you'll grow carefree and a seemingly innocent "new_size = 75/100 * old_size" will devour you.

aj5555
Member #9,033
September 2007

project > properties > c++ > code generation > float point model

type568
Member #8,381
March 2007
avatar

Quote:

There is no such mode in the free version. AFAIK it is only availiable in the enterprise version. Get a real compiler, like GCC ;)

I'm using Microsoft Visual Studio 2005 Professional Edition, not like I'm a professional, but anyways.

Quote:

Audric: If you put integer litterals in float computations, you'll grow carefree and a seemingly innocent "new_size = 75/100 * old_size" will devour you.

I understand.. but in such cases if I remember, I do it 75.0/100, or cast the 100 in to float.

Quote:

aj5555: project > properties > c++ > code generation > float point model

Hmm, how does it work though, if I choose let's say fast. And, does it affect double? Thanks for the tip though.

So, in visual studio, no such option, to see how many time does the program spend in each of the functions.. ?

HoHo
Member #4,534
April 2004
avatar

Quote:

I'm using Microsoft Visual Studio 2005 Professional Edition

I'm not 100% sure but I think Professional edition doesn't have profiler either. Then again I doubt you need to worry about performance that soon.

Quote:

So, in visual studio, no such option, to see how many time does the program spend in each of the functions.. ?

You could manually measure the time by using some high-precision counters (allegro timers are not one of those). Perhaps googling for RDTSC can help you. Though under Windows you also have some other functions whose name I can't remember exactly. It was something like GetXXXFrequency and GetXXXCounter or something like that :)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

aj5555
Member #9,033
September 2007

as far as GCC being a real compiler... MSVC produces faster code (on average)... GCC's strength is in its warning/error, and standards compliance. MSVC is more forgiving (which leads noobs to beleive they are doing things correctly). however as far as code generation goes, you'd have a tough time saying which compiler is better.

type568, float point model is a hint to the compiler about how to handle rounding situations and how to cut-corners when int-float-int conversions are done, if your not uber critical of the results, use fast. you can always select this option on a file by file basis.

do NOT use RDTSC on multi-core machines, your results will not be consistant.
on win32 use QueryPerformanceCounter()

HoHo
Member #4,534
April 2004
avatar

Quote:

as far as GCC being a real compiler... MSVC produces faster code (on average)

Perhaps when your code consists of awfully lot of templates. If you don't have too much of them GCC is one of the best optimizing compilers out there being only a little behind ICC, at least for heavy number crunching. When we compare 64bit compiling then GCC can even beat ICC leaving MSVC far behind.

Quote:

do NOT use RDTSC on multi-core machines, your results will not be consistant.

Are you sure? I somehow remember using it on my older P4 dualcore and it worked fine. Perhaps it is some kind of Linux thing that makes it work though.

Quote:

on win32 use QueryPerformanceCounter()

Ah, that was what it was called :)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

 1   2 


Go to: