Jump to content
  • Advertisement
Sign in to follow this  
MTclip

which is faster - - about function calls?

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

OK wich Method is faster....... which Method is better...... nevermind if some of the syntax is wrong
CtheClass * mtClass = new CtheClass();

somefunction(CtheClass * pClass)
{
  if( pClass->getChair(5)->color != this->m_ColorOfTheSun)
    {
     //blahhhhhhh
    } 
  if( pClass->getChair(5)->color != this->m_ColorOfTheMoon)
    {
     //blahhhhhhh
    } 
  if( pClass->getChair(5)->color != this->m_ColorOfTheOcean)
    {
     //blahhhhhhh
    } 


}


////// OR /////////


somefunction(CtheClass * pClass)
{
  Color *theColor = pClass->getChair(5)->color ;

  if( *theColor != this->m_ColorOfTheSun)
    {
     //blahhhhhhh
    } 
  if( *theColor != this->m_ColorOfTheMoon)
    {
     //blahhhhhhh
    } 
  if( *theColor != this->m_ColorOfTheOcean)
    {
     //blahhhhhhh
    } 


}

/////////// or ///////////

somefunction(CtheClass * pClass)
{
  Color theColor = pClass->getChair(5)->color ;

  if( theColor != this->m_ColorOfTheSun)
    {
     //blahhhhhhh
    } 
  if( theColor != this->m_ColorOfTheMoon)
    {
     //blahhhhhhh
    } 
  if( theColor != this->m_ColorOfTheOcean)
    {
     //blahhhhhhh
    } 


}

so if you make reference to a pass class data quite a bit is it faster to create a copy of it to avoid function calls and look ups? ......

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
It depends.
But if you have a lot of tests or a loop AND the data you copy is small (in the case of a color it's alright) it's usually better. Your data is small so it's quick to copy and is on the stack, then you avoid a lot of pointer dereferencing and finally you avoid cache miss as you use stack data.

So I would go with the last option you propose.

The second solution might be useful if you have a long chain of pointer derefencing, and you have a massive objet (lots of members and/or dynamic allocation).

Share this post


Link to post
Share on other sites
All three should be equivelant. As nothing is marked as volatile, any basic optimizing compiler will realise that the value you are using cannot change, and it will perform the lookup once, and cache the result for all of the comparisons. Don't waste your time trying to reuse results of dereferences or expressions; the compiler can figure all that out for you much quicker, and it allows you to keep your code in a form that best expresses your intent.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!