some (useless) remarks about particle.cpp

Content and general development discussion, including quest scripts and server code. TMW Classic is a project comprising the Legacy tmwAthena server & the designated improved engine server based on evolHercules.


Forum rules

This forum houses many years of development, tracing back to some of the earliest posts that exist on the board.

Its current use is for the continued development of the server and game it has always served: TMW Classic.

Post Reply
ManaTux
Peon
Peon
Posts: 3
Joined: 18 Aug 2008, 20:41

some (useless) remarks about particle.cpp

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

Re: some (useless) remarks about particle.cpp

Post 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.
  • 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: 18 Aug 2008, 20:41

Re: some (useless) remarks about particle.cpp

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