some (useless) remarks about particle.cpp

Content and general development discussion, including maps, quests, and server code from the development team.
Post Reply
ManaTux
Peon
Peon
Posts: 3
Joined: Mon Aug 18, 2008 9:41 pm

some (useless) remarks about particle.cpp

Post by ManaTux » Mon Aug 18, 2008 9:54 pm

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 ;)
User avatar
Crush
TMW Adviser
TMW Adviser
Posts: 8046
Joined: Thu Aug 25, 2005 5:08 pm
Location: Germany

Re: some (useless) remarks about particle.cpp

Post by Crush » Mon Aug 18, 2008 10:36 pm

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.
  • former Manasource Programmer
  • former TMW Pixel artist
  • NOT a game master

Please do not send me any inquiries regarding player accounts on TMW.


You might have heard a certain rumor about me. This rumor is completely false. You might also have heard the other rumor about me. This rumor is 100% accurate.
ManaTux
Peon
Peon
Posts: 3
Joined: Mon Aug 18, 2008 9:41 pm

Re: some (useless) remarks about particle.cpp

Post by ManaTux » Mon Aug 18, 2008 11:02 pm

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.
Post Reply