Archived

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

C/C++ optimization question.

This topic is 6361 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
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
Guest Anonymous Poster
Actually using member-data might be slightly faster than accessing data through a struct-pointer, because the ''this'' pointer is passed in the ''ecx'' register, whereas the struct-pointer will be passed on the stack.
Probably very small differences in an optimized build, maybe...

Share this post


Link to post
Share on other sites
OK, for those of you who''ve never written a compiler, the global variables SHOULD be faster, because they are in the global scope (accessable to all functions, put in a separate area of memory). However, you have to remember that unless you''re accessing a lot of global variables in that area frequently, it''s possible that accessing a global variable will cause a page fault, which means a whole 8k block will have to be fetched from memory. There''s also the cache miss penalty to take into account, too. However, as I said, this only applies if you''re not using global variables (on the same page) very frequently.

Vyvyan

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Shouldn''t it be easiest to use parameters in some way (instead of globals) since you''re doing recursive functions, most recursive functions I''ve written reqwuire their own stack-frame of data, so as to not destroy the data in previous calls....

Don''t know if this apply to you but anyway...

Share this post


Link to post
Share on other sites
Ok, now we''re gonna get into registers, cahces, page faults, etc. etc. etc.

I''ll just write a small program when I get the chance, and post the results here.

Share this post


Link to post
Share on other sites
Using a recursive function is not the way to go when you want to you have a fast executing program.

-------------------------
That's just my 200 bucks!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
What about using

inline func1( const int &x, const int &y, const int &z )
{
//use x
//use y
//use z
}

this way you avoid creating copies from the parameters, and specifying them as CONSTS, the compiler can optimize it.


André

André Luiz Silva
"There is no knowledge that is not power"

Share this post


Link to post
Share on other sites
I agree with Gladiator.
Don''t use recursion. Almost all cases where someone thinks they need it, they don''t.
Post a little code showing the recursion. We can probably get rid of it. (Speed increase + less danger of running out of mem)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Andre Luiz Silva:

Using refernces to ints like that will as you said not create more copies of the variables x, y, z, it will however create 3 pointers to ints (references and pointers are the same at the low/assembly level) and 3 pointers take up exactly as much space as 3 ints, with the added speed-impact of doing pointer derefrences whenever the value of x, y, z is needed

so Simply passing the parameters as ints would be more efficient.

Share this post


Link to post
Share on other sites
Guys, I appreciate the support, but I am implementing ROAM terrain tesselation, and I highly doubt that you can come up with something that will substitute recursion that will be barely readable, nevermind maintainable
Unfortunately I am stuck with the recursion, and I want to optimize it as well as I can. I am far from finished with the algorithm, but I am getting there, and I want to start thinking about possibilities to optimize now.

Share this post


Link to post
Share on other sites
I''m not a 100% sure about this one, but why not implement in a class? Maybe something like:
    
class ROAM
{
private:
static int x,y,z;
public:
void __fastcall func() const;
};


Share this post


Link to post
Share on other sites