Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

speed reference vs by value

i have following question. I tried assignment by value and by reference and as stated here assignment ba value should be faster although when I tried my code it gave me a rather mixed results, as sometimes assign1 is faster, sometimes assign2.

    class MyAddress{
    char *name;
    long int number;
    char *street;
    char *town;
    char state[2];
    long zip;
    std::vector<int> v_int;
public:
    MyAddress(int i){
        v_int.resize(1000000);
        std::fill(v_int.begin(),v_int.end(),i);
    }
    MyAddress& assign1(MyAddress const& x)
    { 
        MyAddress tmp(x);          // copy construction of tmp does the hard work
        std::swap(*this, tmp);  // trade our resources for tmp's
        return *this;      // our (old) resources get destroyed with tmp 
    }
    MyAddress& assign2(MyAddress x)//x is a copy of the source; hard work already done
    { 
        std::swap(*this, x);  // trade our resources for x's
        return *this;      // our (old) resources get destroyed with x 
    }
};

main:

for(int i=0;i<10;i++){
        {
            MyAddress a1(1);
            MyAddress a2(2);
            MyAddress a3(3);
            clock_t tstart=std::clock();
            a1.assign1(a2);
            a1.assign1(a3);
            clock_t tend=std::clock();
            float time_elapsed=((float)tend-(float)tstart);
            std::cout<<std::fixed<<"\nassign1 time elapsed : "<<time_elapsed/CLOCKS_PER_SEC;
        }
        {
            MyAddress a1(1);
            MyAddress a2(2);
            MyAddress a3(3);
            clock_t tstart=std::clock();
            a1.assign2(a2);
            a1.assign2(a3);
            clock_t tend=std::clock();
            float time_elapsed=((float)tend-(float)tstart);
            std::cout<<"\nassign2 time elapsed : "<<time_elapsed/CLOCKS_PER_SEC;
        }
    std::cout<<std::endl;
    }

assign1 time elapsed : 0.093000 assign2 time elapsed : 0.094000

assign1 time elapsed : 0.095000 assign2 time elapsed : 0.092000

assign1 time elapsed : 0.109000 assign2 time elapsed : 0.093000

assign1 time elapsed : 0.099000 assign2 time elapsed : 0.094000

assign1 time elapsed : 0.099000 assign2 time elapsed : 0.101000

assign1 time elapsed : 0.096000 assign2 time elapsed : 0.120000

assign1 time elapsed : 0.098000 assign2 time elapsed : 0.105000

assign1 time elapsed : 0.113000 assign2 time elapsed : 0.108000

assign1 time elapsed : 0.111000 assign2 time elapsed : 0.103000

assign1 time elapsed : 0.106000 assign2 time elapsed : 0.106000

I made some changes to the testing code: now I perform 1000 iterations instead of 10. the results are still mixed: what is specially strange for me is that sometimes first assign is faster but sometimes second:

1)
assign1 time elapsed : 111.228996
assign2 time elapsed : 112.097000
2)
assign1 time elapsed : 127.087997
assign2 time elapsed : 126.691002

how do you explain this? for me it looks like the result is independent of the value or reference method in this case.


finally I have used something like this as I think this is the method suggested here if I understand it correct

MyAddress get_names(MyAddress& ref){return ref;}

and now in assign2 it is done this way:

a1.assign2(get_names(a2));
a1.assign2(get_names(a3));

in fact the performance is slightly better with assign2, at least each time, not interchangeably as before. but is this the difference that I was supposed to see?

assign1 time elapsed : 127.087997
assign2 time elapsed : 126.691002

assign1 time elapsed : 137.634995
assign2 time elapsed : 136.054993

finally:

assign1 time elapsed : 1404.224976
assign2 time elapsed : 1395.886963
like image 385
4pie0 Avatar asked Nov 30 '25 16:11

4pie0


2 Answers

Both your assign functions are basically the same. In one you copy-construct the temporary yourself, while in the other one the copy is automatically injected by the compiler.

Also 10 is not a big enough factor for your testing.

like image 50
K-ballo Avatar answered Dec 02 '25 07:12

K-ballo


For a generic case,
Assignment by reference should almost always outperform assignment by value.

Reason :

  1. Assignment by reference does not create new space on stack
  2. It does not have to copy the value from old variable to new variable

The above overhead may be insignificant for most of the programs. However, a const and reference is a general programming practice.

In the current program,
Both the configurations will perform same. Try your program with a large number of iterations to see the difference in the timings.

like image 32
prathmesh.kallurkar Avatar answered Dec 02 '25 07:12

prathmesh.kallurkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!