Speed โ€‹โ€‹relative to value

I have the following question. I tried assignment by value and by reference and, as indicated here, assigning the value of ba should be faster, although when I tried my code, it gave me pretty mixed results, because 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 
    }
};

home:

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 has passed: 0.093000 assign2 time has passed: 0.094000

assign1 elapsed time: 0.095000 assign2 elapsed time: 0.092000

assign1 time has passed: 0.109000 assign2 time has passed: 0.093000

assign1 elapsed time: 0.099000 assign2 elapsed time: 0.094000

assign1 elapsed time: 0.099000 assign2 elapsed time: 0.101000

assign time elapsed: 0.096000 assign2 time elapsed: 0.120000

assign1 time has passed: 0.098000 assign2 time has passed: 0.105000

assign1 : 0.113000 assign2 : 0.108000

assign1 : 0.111000 assign2 : 0.103000

, : 0.106000 assign2 : 0.106000

: 1000 10. : , , :

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

? , .


, - , , , ,

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

assign2 :

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

assign2, , , . , ?

assign1 time elapsed : 127.087997
assign2 time elapsed : 126.691002

assign1 time elapsed : 137.634995
assign2 time elapsed : 136.054993

:

assign1 time elapsed : 1404.224976
assign2 time elapsed : 1395.886963
+3
2

. - , .

10 .

+6

,
.

:

. .

,
. , .

+1

All Articles