insight on subroutine and global variable

Anyone generous enough to share knowledge and insight in truthfulness on comparing efficiencies and speed between subroutine/function and global variable as being implemented to H/W

Subroutine/function or local variable so commonly known, by some c/c++ experts, being inside the stack (frame) while global variable is in heap.

And they or some argue that the one in stack will be shoved into CPU's cache so get more efficient ie. faster than that of on DRAM

How true is the way really as expected above when c/c++ function/local and global variable have been compiled to assembly or CPU binary then go on implemented in such ?

How to prove and to measure the speed/efficiency advantage ? Thanks before.


You are asking questions that have absolutely nothing to do with C++ and everything to do with a specific processor's architecture.

Further, unless you are doing something weird, accessing a global vs a local variable should, cache/architecture issues aside, not have a significant difference in access speed or "efficiency".

As for the CPU cache, it is not dependent on whether an object is on the stack or the heap, but whether the object (or one near it) was recently accessed, thus bringing the object into the cache.

As with all such questions, it is meaningless to consider them outside of a specific test profile. Always profile your program to determine where bottlenecks occur. It will almost never be simply accessing a variable. (Unless, again, you have done something weird.)
Topic archived. No new replies allowed.