Quote:Original post by SiCrane
1) For primitives allocating variables on the stack takes effectively no time at all, and using the static variable will probably not do good things to the cache. On the other hand, for classes with a non-primitive constructor it may save time, but again will still not be cache friendly. However, this falls firmly in the realm of micro-optimization. There are better things to concentrate your time on.
Right.
Imagine how the program lies in the memory.
Each process has its own address space.
This address space is mapped onto the virtual address space and this one is mapped onto the physical address space
The program is seperated into 4 sections
code segment: where the code lies
data segment: where all constants, static variables ... lie
heap segment: here are the dynamic allocated objects
stack segment: your program stack
Now in order to use the static variable you have to load its page from the data segment into the cache if it isn t there already.
Now dependent on your machine and its addressing operations you either copy the result of GetTime() directly to object's memory in the cache
or you need to load the content from the stack into a register and move it from the register into the cache.
And as already mention above multithreading will be a problem.
In general avoid static variables, they may lead to hidden bug sometimes, plus they lead the cache trashing if they aren t referenced very often. At least they waste space in the cache (4kb per page in general depending on your OS)