Advertisement

globals faster? Andre LaMothe says...

Started by August 07, 2000 11:31 AM
2 comments, last by rowbot 24 years, 4 months ago
hey in "Tricks of the Windows Game Programming Gurus" Andre LaMothe says that using global variables is faster than passing variables as parameters to functions because of the pushing and popping on the stack. But... wouldnt the hit for accessing the memory that the global variable is contained in be even worse? Does anybody know why this is? Does it hold true in all cases? Is it the same for C++ methods? cheers ro
This is really one of those profile before you optomise cases. However the stack tends to show strong "locality of reference" and thus you have a very good chance that whatever you read from the stack was written recently, thus a cache hit. Compilers will also often pass things in registers, rather than the stack (though not so much on x86). Methods work exactly like functions, you just have the implied/hidden *this.

Basically any function that''s "hot" enough that the stack overhead is starting to squeek should be inline. For cases where many things share a large amount of data, and you will only want one instance of that data, use static members.
Advertisement
This also completely depends on the CPU.

There are lots of things that may be faster but aren''t going to give you any speed. This is one of them. And, if you have a function that gets called constantly, you can either inline it (much faster than anything else), or use the fastcall convention, which passes two arguments through registers.

-RWarden (roberte@maui.net)

This topic is closed to new replies.

Advertisement