Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:19:50+00:00 2026-06-04T04:19:50+00:00

i have following question. I tried assignment by value and by reference and as

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:19:51+00:00Added an answer on June 4, 2026 at 4:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have downloaded sample code from Apple Center.I also have gone through following question:
Sometimes I have seen following code. gem 'factory_girl','= 1.2.3' require 'factory_girl' I tried to
I have the following code: <div class=faq> <h1 class=question>Question</h1> <p class=answer>Answer</p> </div> <div class=faq>
I have the following question regarding Flex/AIR data grids: Can I access the value
In interview I have been asked following question. I tried to answer the question
I have the following question about JPA: Can I save the order of the
I'm working for a university project, and I have the following question: I have
I have gone through following question. Convert NSString to NSDictionary It is something different
I have seen the following question but I still have a few doubts. Sending
I have following event class. I have a question related to the Property method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.