Thread: To pass by ref or not to.

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

    To pass by ref or not to.

    To pass build in types by reference.

    Code + Asm
    Code:
    	1	#include <iostream>
     	2	
     	3	void function(const int a)
    -	4	{
    -	5	    int x = a;
    -	6	}
     	7	
     	8	int main()
    -	9	{
    -	10	    function(10);  
    -	11	    return 0;
    -	12	}
    Code:
    -	0x40139e	<main>:		push   ebp
    -	0x40139f	<main+1>:		mov    ebp,esp
    -	0x4013a1	<main+3>:		sub    esp,0x8
    -	0x4013a4	<main+6>:		and    esp,0xfffffff0
    -	0x4013a7	<main+9>:		mov    eax,0x0
    -	0x4013ac	<main+14>:		add    eax,0xf
    -	0x4013af	<main+17>:		add    eax,0xf
    -	0x4013b2	<main+20>:		shr    eax,0x4
    -	0x4013b5	<main+23>:		shl    eax,0x4
    -	0x4013b8	<main+26>:		mov    DWORD PTR [ebp-4],eax
    -	0x4013bb	<main+29>:		mov    eax,DWORD PTR [ebp-4]
    -	0x4013be	<main+32>:		call   0x40d080 <_alloca>
    -	0x4013c3	<main+37>:		call   0x40ccc0 <__main>
    -	0x4013c8	<main+42>:		mov    DWORD PTR [esp],0xa
    -	0x4013cf	<main+49>:		call   0x401390 <_Z8functioni>
    -	0x4013d4	<main+54>:		mov    eax,0x0
    -	0x4013d9	<main+59>:		leave  
    -	0x4013da	<main+60>:		ret
    And by reference
    Code:
     	1	#include <iostream>
     	2	
     	3	void function(const int &a)
    -	4	{
    -	5	    int x = a;
    -	6	}
     	7	
     	8	int main()
    -	9	{
    -	10	    function(10);  
    -	11	    return 0;
    -	12	}
    Code:
    -	0x4013a0	<main>:		push   ebp
    -	0x4013a1	<main+1>:		mov    ebp,esp
    -	0x4013a3	<main+3>:		sub    esp,0x18
    -	0x4013a6	<main+6>:		and    esp,0xfffffff0
    -	0x4013a9	<main+9>:		mov    eax,0x0
    -	0x4013ae	<main+14>:		add    eax,0xf
    -	0x4013b1	<main+17>:		add    eax,0xf
    -	0x4013b4	<main+20>:		shr    eax,0x4
    -	0x4013b7	<main+23>:		shl    eax,0x4
    -	0x4013ba	<main+26>:		mov    DWORD PTR [ebp-8],eax
    -	0x4013bd	<main+29>:		mov    eax,DWORD PTR [ebp-8]
    -	0x4013c0	<main+32>:		call   0x40d080 <_alloca>
    -	0x4013c5	<main+37>:		call   0x40ccc0 <__main>
    -	0x4013ca	<main+42>:		mov    DWORD PTR [ebp-4],0xa
    -	0x4013d1	<main+49>:		lea    eax,[ebp-4]
    -	0x4013d4	<main+52>:		mov    DWORD PTR [esp],eax
    -	0x4013d7	<main+55>:		call   0x401390 <_Z8functionRKi>
    -	0x4013dc	<main+60>:		mov    eax,0x0
    -	0x4013e1	<main+65>:		leave  
    -	0x4013e2	<main+66>:		ret
    Now by just looking at the ASM it seems that calling a built in type by passing not by reference is a faster method, is this true or am I just not interperting this right. No, I don't care that much about speed more of a knowledge question.
    Woop?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    In terms of

    Code:
    -	0x4013ca	<main+42>:		mov    DWORD PTR [ebp-4],0xa
    -	0x4013d1	<main+49>:		lea    eax,[ebp-4]
    -	0x4013d4	<main+52>:		mov    DWORD PTR [esp],eax
    -	0x4013d7	<main+55>:		call   0x401390 <_Z8functionRKi>
    It just stores it in a local place on the stack and then passes a pointer of it to the function, instead of just passing the value by itself. Nothing faster really, maybe more operations.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, 10 is not an object, so passing by reference really shouldn't be used. I believe a temporary must be created.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well I mostly understood the ASM. Is this a preference thing or is it "better" to pass a built in by value and not by reference. I thought I saw somewhere that built in types were best passed by value and not by reference.
    Last edited by prog-bman; 04-17-2006 at 07:37 PM.
    Woop?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Isn't this all just implementation-specifics? Like at one time the XOR hack was en vogue, and common knowledge was to never compare to zero? I suppose that references are implemented much like pointers, and calling conventions and such will differ between pointers and other types.

    A general guideline today may just be tomorrow's folly. I believe the more prudent approach is to code correctly first. Then profile to find bottlenecks. Then apply only necessary optimizations. And add micro-optimizations only as a last resort, with plenty of comments as to the actual measured reasons as to why they are necessary.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    http://www.awprofessional.com/articl...73338&seqNum=2

    Above is some commenting on the subject from the guide to C++ best practices, which states to pass built-in and cheaply copied objects by value, although it doesn't go into much detail on the why's.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by Dave_Sinkula
    Isn't this all just implementation-specifics? Like at one time the XOR hack was en vogue, and common knowledge was to never compare to zero? I suppose that references are implemented much like pointers, and calling conventions and such will differ between pointers and other types.

    A general guideline today may just be tomorrow's folly. I believe the more prudent approach is to code correctly first. Then profile to find bottlenecks. Then apply only necessary optimizations. And add micro-optimizations only as a last resort, with plenty of comments as to the actual measured reasons as to why they are necessary.
    This was for knowledge I was just wondering. I know that worring about passing a built in by reference or value would be a last resort speed up tactic. Sorry I just like to be informed on the little tid bits
    Woop?

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Let's put this into perspective. Obviously it wouldn't make sense to pass a char by reference in any case to a function unless one really wants to modify it, as a char is 1 byte and a pointer can be anywhere between 4 and 8 bytes (and possibly 16 if Intel makes a chip for it ). Even with an int, which takes up the amount of space equal to a pointer, the extra reference/dereferencing calls add overhead. In fact, on some systems you might be better off just doing a x = fn(x); for speed.

    On the other hand, if you want to pass a 2000 digit number into a crypto function, passing by reference would be a very viable option.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Sorry I just like to be informed on the little tid bits
    It's not wrong to be curious about best practices. Obviously trying to optimize by changing how you pass a built-in variable is probably futile, but learning what the common and/or best practice is in that regard, and why it is so, is certainly worthwhile.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I did some timing on this, just for the sake of it (dont have any results at hand). And it seems that it all varies...some runs it was faster to pass by reference, some it was faster to pass by value. Couldnt find a real pattern either so the only conclusions I can draw are that either the testing wasnt good enough or they are almost equally fast and it is stuff that you cant really control in your program (such as other processes taking a tiny bit of CPU just at that moment) that makes up the difference.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by prog-bman
    This was for knowledge I was just wondering. I know that worring about passing a built in by reference or value would be a last resort speed up tactic. Sorry I just like to be informed on the little tid bits
    Yes, and such things are good. Especially since you've already done much homework.

    [blather]
    But again, some things do change. So I think things like, "Prefer taking inputs of primitive types (e.g., char, float ) and value objects that are cheap to copy (e.g., Point, complex<float> ) by value." are also based upon the frame in time in which they are written. As I mentioned, some things that may be as "obvious" as this may one day not be so.

    Case in point: a pointer is always bigger than or char. Well, not guaranteed to be true. Or even that an int and a pointer have the same size for that matter. So you could write piles of code that take advantage of this "obvious fact", and two generations of processors later your code may look like it was written by a void main()'er.
    [/blather]

    The underlying reasons most likely lie in the sizes of the underlying data (pointers or not), and calling conventions. And sizes do vary with implementation. So I suppose the general guidelines assume you know your implementation(s).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. C function - Pass by value? Pass By Ref?
    By stevong in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:02 AM
  3. HowTo increase 1 value to the pointer and pass by ref?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 05:21 PM
  4. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM