Skip to main content
gamedev.net
🔒 Locked

arbitrary values.

Started by Verb Jan 7, 2003 at 2:33 PM 5 replies 250+ views
Original Post
Verb
Verb
I would assume that using a power of two when an arbitrary value is required for multiplication and division would be favourable, considering all that is required is simple bit-shifting rather than repeated additions/subtractions. Does anyone know if this is actually the case? I thought i would ask here before wasting half an hour by testing this myself.
Verb
Verb
I ended up testing this after all. For anyone that is interested, here are the results. It only takes approx 200ms more to perform a thousand-million divisions by 123 than by 128.


Beginning Benchmark...
1,000,000,000 divisions by 128: 3836
1,000,000,000 divisions by 128: 3795
1,000,000,000 divisions by 128: 3796
1,000,000,000 divisions by 128: 3805
1,000,000,000 divisions by 128: 3775
1,000,000,000 divisions by 128: 3786
1,000,000,000 divisions by 128: 3775
1,000,000,000 divisions by 128: 3756
1,000,000,000 divisions by 128: 3775
1,000,000,000 divisions by 128: 3816
-------------------------------------
1,000,000,000 divisions by 123: 4086
1,000,000,000 divisions by 123: 4075
1,000,000,000 divisions by 123: 4066
1,000,000,000 divisions by 123: 4126
1,000,000,000 divisions by 123: 4106
1,000,000,000 divisions by 123: 4086
1,000,000,000 divisions by 123: 4106
1,000,000,000 divisions by 123: 4056
1,000,000,000 divisions by 123: 4066
1,000,000,000 divisions by 123: 4085
Press any key to continue



[edited by - Verb on January 7, 2003 4:35:30 PM]

[edited by - Verb on January 7, 2003 4:41:52 PM]
Verb
Verb
Yes, i noticed that, i was resseting the counter outside the loop. (silly me!)

i have re-run the test and corrected the post

[edited by - Verb on January 7, 2003 4:44:54 PM]
Miserable
Miserable
quote:
Original post by Anonymous Poster
Any good compiler will optimize /128 to a right-shift, so you can as well type /128. If not, you should look for a new compiler.


I wonder ... are you positive that a right-shift would truly be faster than an integer division on all architectures? This was certainly a common optimisation trick in the days of yore, but on today''s processor it''s not only unnecessary preoptimisation (as you pointed out); I''m not at all sure it''s always beneficial. On the other hand I am no expert: This is a question. (Pentium 4, lack of barrel shifting ...?)

Assuming that right-shifting is not necessarily faster than integer division, I would be surprised to see compilers ''optimise'' in a way that might actually slow the code down.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.