Sign in to follow this  

an increase of 0.5ms per frame

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

Quote:
Original post by gamefish
Are member variables really slower than gloables?


Yes, I think. The compiler has to insert a little arithmetic into the code when you access a member variable of a class. How much I'm uncertain of.

Share this post


Link to post
Share on other sites
Quote:
Original post by smr
Quote:
Original post by gamefish
Are member variables really slower than gloables?


Yes, I think. The compiler has to insert a little arithmetic into the code when you access a member variable of a class. How much I'm uncertain of.


... and you are right to be uncertain. A variable is a variable. Wether it is in a class or not is not a problem for the compiler. If the variable is on the stack (for example, a local variable), it address is [(stack pointer)+n]. If the variable is an object and you need to access to a member of this object, its adress is [(stack pointer)+n+p]. The main point here is that both n and p are constants. Thus, it finaly becomes [sp+K], where k is a constant and sp is stored in a register.

The same kind of calculus is done if the variable is in the heap.

Now, you can try to compare a heap variable and a stack variable. This comparison is very difficult because you have to take a lot of factors in account: variable locality (it is faster to access to two contiguous memory zone than to two distant ones, because the processor has to flush its data cache and refill it), variable size and alignement, and so on.

smr question is a bit strange: member variables and globals are not related - after all, if a global variable is a class instance, its member variables are also globals.

HTH,

Share this post


Link to post
Share on other sites

This topic is 4582 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this