
Newbie pointer/address question
Quote:
> Is it faster to send the address of a variable to a function, and within
> that function just reference a pointer to the variable (ex1) or is it faster
> to pass the entire variable (ex2)?
For objects whose size is no greater than
the size of a pointer, and where there is
no expensive copy operation, passing by
value is generally faster, assuming the
value will actually be used. (Memory
caching effects could easily come into
play if the value is not used and the
address is in already cached memory.)
As always, performance issues that really
matter deserve testing. It is fine, albeit
often premature, to worry about this from a
theoretic perspective, but performance is
notoriously hard to predict.
Quote:
> And can either method cause memory
> overwriting problems (just passing DWORDs, longs -i.e. no char arrays)?
> Thanks!
> ex1:
> int main(void)
> {
> boolAns = IsProcessRunning(&dwPID);
"Push" one 4-byte object, whose bits are probably taken
from the instruction stream and a register. (fast)
The "push" is probably a register modify or a write to
stack memory, nearly always in cache. (fast)
Quote:
> }
> BOOL IsProcessRunning(DWORD *dwPID)
> {
> somefunction(*dwPID);
Dereference pointer taken from stack frame or
likely cached memory. Getting the pointer is
fast. Dereferencing it can be fast or slow
depending on where it points.
Quote:
> }
> ex2:
> int main(void)
> {
> boolAns = IsProcessRunning(dwPID);
Retrieve one 4-byte object from memory. Depending
on where that is, this could be fast or slow.
Quote:
> }
> BOOL IsProcessRunning(DWORD dwPID)
> {
> somefunction(dwPID);
Retrieve one 4-byte object from stack frame or
register. This will tend to be fast, even if
from stack frame which is normally in cache.
Quote:
> }
--
-Larry Brasfield
(address munged, s/sn/h/ to reply)