Page 1 of 1

some (useless) remarks about particle.cpp

Posted: 18 Aug 2008, 20:54
by ManaTux
just looked at the code...

SIN45 is already defined in <cmath>
----> #define SIN45 M_SQRT1_2

in addTextSplashEffect
three call to rand and three modulos... can be done more efficiently:
instead of

Code: Select all

newParticle->setVelocity(((rand() % 100) - 50) / 200.0f,    // X
                         ((rand() % 100) - 50) / 200.0f,    // Y
                         ((rand() % 100) / 200.0f) + 4.0f); // Z
I would recommand

Code: Select all

int r=rand()>>4;
newParticle->setVelocity((( r      & 127) - 64) / 256.0f,    // X
                         (((r>>8 ) & 127) - 64) / 256.0f,    // Y
                         (((r>>16) & 127) / 256.0f) + 4.0f); // Z
Ok, you're right, this is far from critical... cf. the subject of my post ;)

Re: some (useless) remarks about particle.cpp

Posted: 18 Aug 2008, 21:36
by Crush
In case of non-performance critical code (this particular piece of code is executed only once everytime a damage number is generated) code readability is more important than efficiency. "(rand() % 100) - 50" immediately tells you "random number between -50 and 50" while the meaning of "((r>>8 ) & 127) - 64" is not that apparent on the first look.

And although slow divisions by powers of two can be optimized by the compiler by substituting them with fast bitwise shifts in integer arithmetic this is not the case when dealing with floating point numbers.

Re: some (useless) remarks about particle.cpp

Posted: 18 Aug 2008, 22:02
by ManaTux
the point was not to remove the slow division, but the slow modulo :)
and most of all replace three calls to rand with one.

But I agree with you, it might be less readable.