Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

billybob

redundant casts and performance

This topic is 5595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

do redundant casts hurt performance: typedef double MyFloat MyFloat = (MyFloat)sqrt(...); will that cause any slowdown? i''m under the impression that casts between floats and doubles is a compile time thing, but i''m not sure.

Share this post


Link to post
Share on other sites
Advertisement
As far as I can see from examining an assembly listing output by compiling double var = (MyFloat)sqrt(99); vs. double var = sqrt(99); there is no difference in the assembly output therefore no difference speedwise or sizewise of the output executable. I tested this with optimizations off and on and both note no difference between the casted and non-casted code.

-- Exitus Acta Probat --

Share this post


Link to post
Share on other sites
Redundant casts will never in any situation hurt performance. Compilers happily ignore such things. On a similar note, most ''optimized'' code is actually more verbose/lengthy than unoptimized code. This even goes for interpreted bytecode languages like Java/C# and VB (although to a lesser extent). Go figure.

The only time a cast would affect performance is if you had non-redundant casts.. ie, purposfully casted something to (short int) or (char) before assigning it to a regular int v; type variable. In these cases, the math would be done in a short or char variable (which is inherently slower than ints), or truncated using a bitwise operation. Either way, the impact on performance (1 to 3 instructions) is somewhere below miniscule outside of just about anything short of code that is run on a per-pixel basis during the rendering of a scene (ie software 2D/3D rendering).

- Air

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!