slight performance suggestion

View previous topic View next topic Go down

slight performance suggestion

Post  freebird373 on Thu Mar 12, 2009 12:50 am

Wherever you use float division you could replace it with float multiplication (faster).
for example:
_SIO2particle->speed = (float)sio2Randomui( (unsigned int)MAX_SPEED ) / 100.0f;
could be
_SIO2particle->speed = (float)sio2Randomui( (unsigned int)MAX_SPEED ) * 0.01f;

Not much of a factor, but it all adds up .

freebird373

Posts : 10
Join date : 2008-11-21

View user profile

Back to top Go down

Re: slight performance suggestion

Post  sio2interactive on Thu Mar 12, 2009 1:22 am

you are absolutely right... I try to do that as much as I could within SIO2, but for the tutorials well that's just for demonstration purpose so... but in any case if you find some within the SIO2 code lemme know!

Cheers,

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: slight performance suggestion

Post  oioioi on Thu Mar 12, 2009 8:12 am

Can I ask why that is faster? Ans why --i is faster than i--. Any reason? Just curious

oioioi

Posts : 136
Join date : 2008-12-02
Location : Norway

View user profile

Back to top Go down

Re: slight performance suggestion

Post  meteors on Thu Mar 12, 2009 8:41 am

Multiplication is always faster than division, for CPU optimization reasons. I'm sure google knows exactly why.

So, instead of dividing by ten, multiply by 0.1, for the same result but faster!




-j
avatar
meteors

Posts : 241
Join date : 2008-11-08
Location : Sunny Florida

View user profile

Back to top Go down

Re: slight performance suggestion

Post  oioioi on Thu Mar 12, 2009 9:24 am

Thanks! That make sens, but --i is faster than i--, that doesn't make sens to me

oioioi

Posts : 136
Join date : 2008-12-02
Location : Norway

View user profile

Back to top Go down

division

Post  aheirich on Thu Mar 12, 2009 12:34 pm

oioioi wrote:Can I ask why that is faster? Ans why --i is faster than i--. Any reason? Just curious

Division is extremely slow in most CPUs, it's a matter of how the circuitry is constructed. Multiplication is always much faster.
(Some CPUs don't even offer hardware support for division, it is emulated in software!)

aheirich

Posts : 11
Join date : 2009-02-25

View user profile

Back to top Go down

Re: slight performance suggestion

Post  spqrusa on Mon Mar 16, 2009 5:00 pm

freebird373 wrote:Wherever you use float division you could replace it with float multiplication (faster).
for example:
_SIO2particle->speed = (float)sio2Randomui( (unsigned int)MAX_SPEED ) / 100.0f;
could be
_SIO2particle->speed = (float)sio2Randomui( (unsigned int)MAX_SPEED ) * 0.01f;

Not much of a factor, but it all adds up .

A few comments regarding this:

1) most good compilers like GCC, which is used for iPhone development, automatically "optimize" division by a constant into multiplication by the inverse constant. I haven't checked this on the iPhone SDK, but I would be shocked if it didn't do this.

2) Multiplication and Division can be made equally as fast on modern hardware. It all depends on the architecture. For iPhone, division is slower than multiplication.

3) The original form is preferable for readability.

SPQR

spqrusa

Posts : 1
Join date : 2009-03-16

View user profile

Back to top Go down

Re: slight performance suggestion

Post  freebird373 on Mon Mar 16, 2009 9:56 pm

Someone should do some time tests ;.

This article is bit outdated but suggests not to use division at all.

http://msdn.microsoft.com/en-us/library/ms839365.aspx

freebird373

Posts : 10
Join date : 2008-11-21

View user profile

Back to top Go down

Re: slight performance suggestion

Post  freebird373 on Mon Mar 16, 2009 10:35 pm

My results from a little test on the 2G ipod touch.

temp = 525.0 / 3.0f;
vs.
temp = 525.0 * 0.333333334f;

One iteration
div: 0.0000099999999748 secs
mult: 0.0000059999999849 secs


10,000 iterations
div: 0.0001499999996213 secs
mult: 0.0001259999996819 secs


eh, NOT much of a difference as expected.


Last edited by freebird373 on Tue Mar 17, 2009 1:41 am; edited 1 time in total

freebird373

Posts : 10
Join date : 2008-11-21

View user profile

Back to top Go down

Re: slight performance suggestion

Post  sio2interactive on Mon Mar 16, 2009 11:34 pm

hummmmmm really interesting... more of theses type of benchmark would be great Wink that can help us all to optimize our code better...

Cheers and tks!,

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: slight performance suggestion

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum