C/C++ optimization question.

Started by
18 comments, last by kill 23 years, 9 months ago
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...
Advertisement
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
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...
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.
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=-..
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"
"- To begin with, said the Cat, a dog's not mad. You grant that? - I suppose so, said Alice. - Well, then, - the Cat went on - you see, a dog growls when it's angry, and wags its tail when it's pleased. Now I growl when I'm pleased, and wag my tail when I'm angry. Therefore I'm mad."
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)
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.
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.
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;};    


This topic is closed to new replies.

Advertisement