Re: Passing by reference vs Passing by value
Re: Passing by reference vs Passing by value
- Subject: Re: Passing by reference vs Passing by value
- From: "Clark S. Cox III" <email@hidden>
- Date: Mon, 30 Sep 2002 16:52:13 -0400
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Monday, Sep 30, 2002, at 15:56 US/Eastern, Arthur Clemens wrote:
In the various Cocoa code I encounter I never see passing by reference
with values like ints or floats.
I learned that Java does not support passing by reference, but
Objective-C should. And technically it works, I found out, although
the code becomes somewhat less readable. Passing by reference should
speed things up a bit, I would think.
Is there an obivous reason not to pass by reference?
Objective-C doesn't have references (at least not in the C++ sense).
Plus, you gain nothing when passing a primitive type by reference
because they are likely being loaded directly into registers, in which
case passing as a pointer (the closest thing that C and Objective-C can
do) would actually slow down the code. For example:
Call by value:
load parameter in register1
branch to called function
//... (inside called function)
operate with parameter already in register1
Call by reference (i.e. pointer)
load parameter's address in register1
branch to called function
//... (inside called function)
load parameter into register2 from location specified by register1
operate with parameter in register2
So, if you pass these primitive types by reference/pointer, you are
actually (conceptually) introducing an additional instruction. This
probably doesn't apply to true C++ references, because, since they are
part of the language, the compiler should be smart enough to put the
parameters directly in registers anyway.
Having said all of that, the issue is moot unless you are doing
function calls/method dispatch inside a tight inner loop, and every
cycle counts (i.e. you've profiled your code, and know for a fact that
the function call/method dispatch is the bottleneck) otherwise, you're
wasting your time and effort.
- --
http://homepage.mac.com/clarkcox3/
email@hidden
Clark S. Cox, III
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (Darwin)
iEYEARECAAYFAj2YuYMACgkQd6STocYT1xUugQCgg+7tmGOhS6fzCTZXY16SOTtq
pl0An2TmKbtps+neuEYJ9NtGh1d0wzAe
=IVqN
-----END PGP SIGNATURE-----
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.