Newbie pointer/address question 
Author Message
 Newbie pointer/address question

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)? 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);

Quote:
}

BOOL IsProcessRunning(DWORD *dwPID)
{
    somefunction(*dwPID);

Quote:
}

ex2:
int main(void)
{
    boolAns = IsProcessRunning(dwPID);

Quote:
}

BOOL IsProcessRunning(DWORD dwPID)
{
    somefunction(dwPID);
Quote:
}



Thu, 27 Oct 2005 00:49:54 GMT  
 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)


Thu, 27 Oct 2005 01:08:19 GMT  
 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)? And can either method cause memory
>overwriting problems (just passing DWORDs, longs -i.e. no char arrays)?

Normally, arguments are passed by value.  The function receives a copy
of the evaluated expression's value.

When passing a type other than pointer, the function only has access
to the copy.  There is no chance of overwriting memory nor can the
called function change a variable in the calling function.

When passing a pointer, the function receives a copy of the pointer's
value (the address it points to).  Using the various dereference
operators and pointer arithmetic, the function could update variables
in the calling function and invoke undefined behavior by overwriting
memory.

The speed issue probably only applies to structures and unions.
(Arrays are always passed by an implied conversion to pointer.)  If
you have a large structure, it is normally faster to pass a pointer to
the structure rather than the structure itself.  (Making a copy of a 4
or 8 byte pointer should be faster than copying a structure of several
hundred bytes.)  If you are concerned about preventing the function
from modifying the structure, declare the pointer as pointing to a
constant struct.

<<Remove the del for email>>



Thu, 27 Oct 2005 01:17:40 GMT  
 Newbie pointer/address question
Thanks, Larry and Barry! I found passing the variable in my controlled tests
took 1/2 as long as passing the address and then pointing to the address.
Wow!


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)? 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);
> }

> BOOL IsProcessRunning(DWORD *dwPID)
> {
>     somefunction(*dwPID);
> }

> ex2:
> int main(void)
> {
>     boolAns = IsProcessRunning(dwPID);
> }

> BOOL IsProcessRunning(DWORD dwPID)
> {
>     somefunction(dwPID);
> }



Thu, 27 Oct 2005 02:01:37 GMT  
 Newbie pointer/address question


Quote:
> Thanks, Larry and Barry! I found passing the variable in my controlled tests
> took 1/2 as long as passing the address and then pointing to the address.
> Wow!

Before you decide what that means, you should
understand that the result could easily change
in a real program.  One of the pitfalls awaiting
performance testers is that a small program and
data set, which can all be accessed from cache
(after the first access), will outperform the
same code running in the context of a larger
program and dataset.  Because cache access is
many times faster than main memory access, the
cache hit rate is often a {*filter*} factor in
performance.

--
-Larry Brasfield
(address munged, s/sn/h/ to reply)



Thu, 27 Oct 2005 05:01:35 GMT  
 Newbie pointer/address question

Quote:
> Before you decide what that means, you should
> understand that the result could easily change
> in a real program.  One of the pitfalls awaiting
> performance testers is that a small program and
> data set, which can all be accessed from cache
> (after the first access), will outperform the
> same code running in the context of a larger
> program and dataset.  Because cache access is
> many times faster than main memory access, the
> cache hit rate is often a {*filter*} factor in
> performance.

As Larry said, it may not be useful to try to determine if one method
of parameter passing is optimal. It's generally regarded
as a bad idea to look at optimization too early in a development
cycle, at least at the low level you're asking about. (It's been
said that premature optimization is the root of all evil :)

Anyway, the most productive optimization is usually at the algorithmic
level,
I.e. is this sort algorithm correct for the type of datasets I have?  Is a
linked
list or an array more appropriate for the patterns of access
(insert/modify/extract etc.)
I expect? Lower level optimizations are better left until profiling can
identify a specific
bottleneck.

HTH,
Steve



Thu, 27 Oct 2005 12:21:40 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. NEWBIE: Copying a memory address and not a pointer

2. pointer to pointer of character ? (newbie question)

3. Newbie question: how to get IP address

4. C Question - pointer & address-operator

5. question about NULL pointer and address 0x00000h

6. Pointer/Address Question

7. DOS, pointers & flat addressing Question

8. Again ... A newbie question (pointers)

9. newbie question: stings, pointers and arrays

10. Pointers and Strings - Newbie Question

11. Newbie question on pointers

12. Newbie question about pointers.

 

 
Powered by phpBB® Forum Software