Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Vanukoff

In general, which is faster?

This topic is 6223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Assuming I have two integers, a and b, and a is much larger than b (a > b). Is it quicker (in code) to multiply a*b or b*a? int a = 424242; int b = 11; int c = a*b; // ? int c = b*a; // ? I thought I read somewhere that order can make a speed difference. Does it?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
If it makes a difference, it''s virtually zero. Floating point numbers can be a different story, though.

Share this post


Link to post
Share on other sites
Even if it did make a noticable difference (which I strongly doubt), then you still wouldn''t know which order the compiler decided to multiply them. Most compilers can perform "mathematical optimizations". Since the compiler ''knows'' that a*b is the same as b*a mathematically, then it might decide to flip the order of the multiplicands in order to simplify register allocation or whatever....

(This would not be the case if operator * is overloaded for the types of ''a'' and ''b'', since "a.operator * (b)" is not necessarily the same as "b.operator * (a)").

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To my understanding mul operations take the same number of cycles regardless of the numbers being used.

You might try making the smaller number a short or char though. But i think multiplication is done with two numbers of the same bit so it would probably convert the numbers to the type with a large bit which would be undesirable. The best way to get more preformance out of a mul operation is todo it with 16 or 8 bit (instead of 32 bit ints) numbers or not do it at all which is even faster.

Share this post


Link to post
Share on other sites
In your specific example, each of those will take exactly the same time because the compiler (assuming it was made within the last 8 years or so) is going to optimize the multiply away and just put the result in c.

And the short answer is that time taken to do a multiply may change due to the size of the type (int, short, long), but its not going to change because of the size of the values within a certain type (with certain exceptions where if you multiply by certain constants the compiler may optimize the operation).


Share this post


Link to post
Share on other sites
An article I read said the first one''s faster, but I did a little test (using ASM, so I knew exactly what was happening) and the second one seemed to be a tiny bit faster. I was doing 3 * 0x7fffffff (1 less than a DWORD can hold) 20,000,000 times per frame though, and the frame rate was the same, except it''d occasionally go up or down 1 frame, and switching them seemed to make it a little less frequent. Not enough to worry about though, and my frame counter probably isn''t too accurate anyway^^



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
To my understanding mul operations take the same number of cycles regardless of the numbers being used.

You might try making the smaller number a short or char though. But i think multiplication is done with two numbers of the same bit so it would probably convert the numbers to the type with a large bit which would be undesirable. The best way to get more preformance out of a mul operation is todo it with 16 or 8 bit (instead of 32 bit ints) numbers or not do it at all which is even faster.



To my understanding I thought 16 and 8 bit numbers were slower than 32 bit numbers, because of the alignment issue on x86 processors. They do save memory, however. If you had a huge structure like 1,000,000 of them then it might be logical to use shorts and MMX instructions, where bit size seems not to matter.

Share this post


Link to post
Share on other sites
First, make sure that you''re not optimizing without a purpose. Can the code run at acceptable speed without caring about this performance issue? I''ve seen people make this optimization blunder too many times, and when it catches up to them, they realize that their competitor produced a satisfactorily-running, full-featured program, while they produced an optimized cookie which is next to impossible to read, much less modify.

Second, if you really care about operation order that much, start an __asm block and do it yourself.

Share this post


Link to post
Share on other sites
According to Andre la Mothe the smaller value should be the
multiplier (right hand value) because the multiplication finishes
as soon as all significant bits of the multiplier have been
processed.

Whether that''s still true on Pentium III I don''t know.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!