Jump to content
  • Advertisement
Sign in to follow this  
iNsAn1tY

How much will this cost me?

This topic is 5473 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

Quick question. I am currently passing translations to my scene objects in two ways:
CVector3 vTranslation(150.0f, 0.0f, 150.0f);
    
m_pSceneObject->SetTranslation(0.0f, 150.0f, 0.0f);
m_pSceneObject->SetTranslation(vTranslation);
Say I wanted to do away with the first function, and pass my arguments like this:
CVector3 vTranslation(150.0f, 0.0f, 150.0f);
    
m_pSceneObject->SetTranslation(CVector3(0.0f, 150.0f, 0.0f));
m_pSceneObject->SetTranslation(vTranslation);
Do I incur any performance overhead for temporarily creating a CVector3 out of the three floats? Obviously, there will be some, but should I be concerned?

Share this post


Link to post
Share on other sites
Advertisement
You shoudlnt be worrying about performance right now. But, since i dont know what is done for temporary variables, whether their just put on the stack as well... or if theyre on the heap, or just on the stack temporarily. But the worst case scenario is that the stack pointer needs to be incremented after your done, in other words, dont worry about it.
-Dan

Share this post


Link to post
Share on other sites
code it both ways. time it. see if you can detect a difference. the only thing you should be using to optimize your code is a profile build. it will give you the time breakdown for each section of your code and you can optimize the worst offenders first.

-me

Share this post


Link to post
Share on other sites
Passing three floats means... three floats on the stack. Passing a vector means... three floats on the stack :) *but* you have the option of using a const reference. Then less data is shuffled, although you have to do a pointer dereference.

In release mode at least, temporaries, constructors, etc. all ought to be inlined nicely (or possibly even removed) by the compiler when it comes to objects this simple. Don't worry about it.

Share this post


Link to post
Share on other sites
I would say don't worry about it for now.

But...
Why not make an overloaded m_pSceneObject->SetTranslation() that excepts an array of floats, or a CVector3 reference? My Vector class can cast to an array of floats (for OpenGL's vector version of functions), so many times if I have a function like this foo(float x, float y, float z), i also have one an overloaded one like this foo(CVector3* vec).

Share this post


Link to post
Share on other sites
Quote:
Original post by Slaru
I would say don't worry about it for now.

But...
Why not make an overloaded m_pSceneObject->SetTranslation() that excepts an array of floats, or a CVector3 reference? My Vector class can cast to an array of floats (for OpenGL's vector version of functions), so many times if I have a function like this foo(float x, float y, float z), i also have one an overloaded one like this foo(CVector3* vec).

Yes, this I what I have at the moment. My SetTranslation() function is overloaded for both CVector3 and three floats. I was just curious about the overhead incurred. Thanks for the replies, I'll try your suggestion, Palidine, and profile it (if MSVC will let me fully compile my program)...

Share this post


Link to post
Share on other sites
If it's not compiling, don't worry about such minor optimizations. Make it run properly first, *then* make it run fast :)

Share this post


Link to post
Share on other sites
Im no compiler guru but i thought when you do this,

m_pSceneObject->SetTranslation(CVector3(0.0f, 150.0f, 0.0f));

the CVector3 would be allocated on the stack during compile time. And i though stack allocation was free?

Share this post


Link to post
Share on other sites
At worst you put it back. That sounds a lot worse than it is. A well tuned application generally has 10% of the code using 90% of the time. With a performance problem it might well be 1% using 99.99%. So it isn't a big chore to find and change the ones that actually need to be changed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!