Advertisement

C/C++ optimization question.

Started by July 16, 2000 08:33 PM
18 comments, last by kill 24 years, 5 months ago
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.
Don''t use either of those methods, use pointers.

Nate Miller
http://nate.scuzzy.net
Advertisement
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'."
- The Goblin (madgob@aol.com)
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.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
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
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
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

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

Advertisement
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=-..
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.
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.)
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.

This topic is closed to new replies.

Advertisement