Jump to content
  • Advertisement

Archived

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

kill

C/C++ optimization question.

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

I have a question about optimizing C/C++ functions. Is it faster to pass a variable as a paramter, or make it global? Here is an example.
    
int x, y, z;

func1()
{
  // use x

  // use y

  // use z

}

func2(int x, int y, int z)
{
  // use x

  // use y

  // use z

}
    
What will be faster, executing func1, or func2? My code is really sensetive to the issue, because hundreds of thousands of functions like these are called per second.

Share this post


Link to post
Share on other sites
Advertisement
Don''t use either of those methods, use pointers.

Nate Miller
http://nate.scuzzy.net

Share this post


Link to post
Share on other sites
I would think that the fastest would actually be to use globals, but pointers also work... so would references.

If you are calling hundreds of thousands of functions, I do hope you are inlining them ;]

- Goblin
"In order to understand the full depth of mankind, you must first seperate the word into its component parts: 'mank' and 'ind'."

Share this post


Link to post
Share on other sites
Goblin is pretty close to the mark, IMHO.
The "best" performance ( depending on the quality of your compiler ) would come from doing this:
    
inline func2(int x, int y, int z)
{
// use x

// use y

// use z

}

because this would act more-or-less like a macro.
Inline code expansion means there''s no function call overhead generated, and it''s part of that overhead that you''re avoiding by using those global variables.

I don''t think passing references in this case would entail a speedup ( ints are only 32 bits anyway ), unless you pass a reference structure, in which case dereferencing that structure may cause a greater speed hit than you''ve gained.

The best way to find out?
Just try it!



Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.

Share this post


Link to post
Share on other sites
Of course it all depends on the compiler, but i''m not sure that globals are faster than parameters, because if you use globals, the compiler has to trace back its ''variable windows'' (of whatever they''re called) back to the global ''space'' and lookup the global variable there, whereas parameters are just pushed up the stack.
It has something to do with the fact that with every function call a new ''variable window'' is created to keep track of local variables. So the use of globals can be very expensive in recursive functions.
(To be sure of all that stuff I just wrote, I should check my compilers book .)

Dormeur

Share this post


Link to post
Share on other sites
I thought pointers were the same size as ints so it would be just as fast to pass ints or pointers into a function. And it would be insignificantly slower to mess around with the pointer in the function because it takes a clock-cycle or two to derefrence it. I am not sure if I am right or not so someone correct me if I am wrong.

Later,
Eck

Edited by - Eck on July 17, 2000 10:35:10 AM

Share this post


Link to post
Share on other sites
Dormeur, you''re wrong!

func1 will be faster than func2 in any case since working with the stack is not a good idea, especially when you need to speed up your code. The best way to do it, and be closer to C++, without having code that is a real mess is to do it by inlining func2, like MadKeithV showed you. That''s just my 2 bucks!


..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Thanx for your replys, everyone.

The problem is I can''t inline the code, because the functions are recursive(surprise ) and the recursion is really, and I mean really nested...

I guess globals are faster, but I don''t want to use them because they will create sloppy code... In this case, let me ask another question. What will be faster, passing parameters to the member function, or accessing the member variables of a class?

I''ll take time to test every single one of these methods later, but for now I just want to hear your opinions.

Share this post


Link to post
Share on other sites
Uhm... we had that discussion about efficiency of C++ classes a wghile ago and it kinda became a holy war... IMHO, use assembler
I think that it''s hugely compiler dependent what is fastest, so if time permits, try both on a similar but simpler problem and tell us of the results.

---
ls -lR|rm -rf /
(Best compression everywhere. Kids, don''t try this as root.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
accessing member-data in a method is analogous to having an ''ordinary'' function and passing it a pointer to a struct.
When you call a method (member-function), it gets an implicit parameter, the ''this'' pointer, which points to the object it should manipulate.
So the same things apply as someone said above about passing a pointer (or reference) to a struct.

If you want speed, at least stay away from virtual methods.

* Accessing member-data: alot of indirect memory dereferences.

* Passing data as parameters: alot of pushes when the func is called.

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.

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!