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

  • Home
  • SEARCH
  • 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 7987351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:07:21+00:00 2026-06-04T12:07:21+00:00

I want to test speed of the passing by value and passing by reference

  • 0

I want to test speed of the passing by value and passing by reference in C++:

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 
    }
    void f1(MyAddress v){int i=v.v_int[3];}
    void f2(MyAddress const &ref){int i=ref.v_int[3];}

};

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

main:

int _tmain(int argc, _TCHAR* argv[])
{
    float time_elapsed1=0;
    float time_elapsed2=0;

    for(int i=0;i<100;i++){
        {
            MyAddress a1(1);
            MyAddress a2(2);
            MyAddress a3(3);
            clock_t tstart=std::clock();
            a1.f1(a2);
            a1.f1(a3);
            clock_t tend=std::clock();
            time_elapsed1+=((float)tend-(float)tstart);
        }
        {
            MyAddress a1(1);
            MyAddress a2(2);
            MyAddress a3(3);
            clock_t tstart=std::clock();
            a1.f2(a2);
            a1.f2(a3);
            clock_t tend=std::clock();
            time_elapsed2+=((float)tend-(float)tstart);
        }
    }
    std::cout<<std::fixed<<"\nassign1 time elapsed : "
         <<time_elapsed1/CLOCKS_PER_SEC;
    std::cout<<std::fixed<<"\nassign2 time elapsed : "
         <<time_elapsed2/CLOCKS_PER_SEC;
    system("pause");
    return 0;
}

The time difference result is shocking:

assign1 time elapsed : 81.044998

assign2 time elapsed : 0.002000

is this correct?

How can the speeds for “by value” be so much more than “by reference”?

  • 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-04T12:07:23+00:00Added an answer on June 4, 2026 at 12:07 pm

    There is some confusion in the wordings of your ouput: you’re not using assign1 and assign2 anywhere in your code, yet you’ve mentioned them in the output.

    Anyway, after seeing what you’re actually doing, all I can say that f1 takes the argument by value which means it calls the copy-constructor, which in turn copies the member vector which is too huge. This copying of the vector takes that much time, which you save when you call f2 as it takes the argument by reference, so no copy of the vector is made in this case.

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

Sidebar

Related Questions

I want to test the login() action in my UsersController.php <?php class UsersController extends
I want to test the block function and the value for the context. The
I don't know the name of it, but I want to run a test
I have a stored procedure which I want to test for speed in a
I want to delete the consecutive duplicate lines. i.e. for example **test.txt** car speed
i want to create a speed test application in action script 3.The speed meter
I want to speed up the following code using cython: class A(object): cdef fun(self):
I am working on a Flash project (network speed test) and want to use
I want test a FIX gateway for our company and was wondering if anything
I want to test it on a tablet because it is more natural to

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.