Call by value vs. Call by reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Saeed Amrollahi

    Call by value vs. Call by reference

    Dear All
    Hi

    I have learned when an object is big, I should pass the object using a
    reference or pointer to it rather than calling by value.
    In the following code, I don't gain cosiderable performance with call-
    by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
    unconsiderable:

    #include <vector>
    #include <iostream>

    using namespace std;
    void f1(std::vector< intv)
    {
    cout << "f1 called" << '\n';
    for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
    v[sz] = sz;
    cout << "f1 end" << '\n';
    }

    void f2(std::vector< intv)
    {
    cout << "f2 called" << '\n';
    for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
    v[sz] = sz;
    cout << "f2 end" << '\n';
    }

    void f3(std::vector< int>* pv)
    {
    cout << "f3 called" << '\n';
    for (std::vector<in t>::size_type sz = 0; sz < pv->size(); sz++)
    (*pv)[sz] = sz;
    cout << "f3 end" << '\n';
    }
    int main()
    {
    vector<intv(500 00000);
    f1(v);
    f2(v);
    f3(&v);

    return 0;
    }

    I ran under Visual Studio 2005, Visual Studio 2008 and GCC. Is it
    about some compiler switches that I should on/off? Did compilers
    become more clever?

    In advance, thank you for your comments.
    - Saeed Amrollahi
  • Ron AF Greve

    #2
    Re: Call by value vs. Call by reference

    Hi,


    I would expect you see a large difference since if you pass by value it has
    to copy the whole vecor, however..

    In your code the compiler might have removed the whole vector copying during
    optimization since you don't do anything with it. You wouldn't notice a
    difference between a program with the vector passed in and one completely
    without and without assignment

    Regards, Ron AF Greve



    "Saeed Amrollahi" <s_amrollahi@ya hoo.comwrote in message
    news:7159c59a-49aa-46b0-938f-e0cb4d003701@w7 g2000hsa.google groups.com...
    Dear All
    Hi
    >
    I have learned when an object is big, I should pass the object using a
    reference or pointer to it rather than calling by value.
    In the following code, I don't gain cosiderable performance with call-
    by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
    unconsiderable:
    >
    #include <vector>
    #include <iostream>
    >
    using namespace std;
    void f1(std::vector< intv)
    {
    cout << "f1 called" << '\n';
    for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
    v[sz] = sz;
    cout << "f1 end" << '\n';
    }
    >
    void f2(std::vector< intv)
    {
    cout << "f2 called" << '\n';
    for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
    v[sz] = sz;
    cout << "f2 end" << '\n';
    }
    >
    void f3(std::vector< int>* pv)
    {
    cout << "f3 called" << '\n';
    for (std::vector<in t>::size_type sz = 0; sz < pv->size(); sz++)
    (*pv)[sz] = sz;
    cout << "f3 end" << '\n';
    }
    int main()
    {
    vector<intv(500 00000);
    f1(v);
    f2(v);
    f3(&v);
    >
    return 0;
    }
    >
    I ran under Visual Studio 2005, Visual Studio 2008 and GCC. Is it
    about some compiler switches that I should on/off? Did compilers
    become more clever?
    >
    In advance, thank you for your comments.
    - Saeed Amrollahi

    Comment

    • Soumen

      #3
      Re: Call by value vs. Call by reference

      On Jul 11, 1:37 pm, Saeed Amrollahi <s_amroll...@ya hoo.comwrote:
      Dear All
      Hi
      >
      I have learned when an object is big, I should pass the object using a
      reference or pointer to it rather than calling by value.
      In the following code, I don't gain cosiderable performance with call-
      by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
      unconsiderable:
      >
      #include <vector>
      #include <iostream>
      >
      using namespace std;
      void f1(std::vector< intv)
      {
              cout << "f1 called" << '\n';
              for (std::vector<in t>::size_type sz = 0; sz < v.size();sz++)
                      v[sz] = sz;
              cout << "f1 end" << '\n';
      >
      }
      >
      void f2(std::vector< intv)
      {
              cout << "f2 called" << '\n';
              for (std::vector<in t>::size_type sz = 0; sz < v.size();sz++)
                      v[sz] = sz;
              cout << "f2 end" << '\n';
      >
      }
      >
      void f3(std::vector< int>* pv)
      {
              cout << "f3 called" << '\n';
              for (std::vector<in t>::size_type sz = 0; sz < pv->size(); sz++)
                      (*pv)[sz] = sz;
              cout << "f3 end" << '\n';}
      >
      int main()
      {
              vector<intv(500 00000);
              f1(v);
              f2(v);
                      f3(&v);
      >
              return 0;
      >
      }
      >
      I ran under Visual Studio 2005, Visual Studio 2008 and GCC. Is it
      about some compiler switches that I should on/off? Did compilers
      become more clever?
      >
      In advance, thank you for your comments.
       - Saeed Amrollahi
      Probably because your vector is empty (may be it's just holding a
      pointer and few other int)and you're inserting elements inside the
      function.
      Try inserting elements before calling the function and call the
      function 1000 times or so. Also try vector of string and let it have
      all same
      elements. Let me know what you find.

      Regards,
      ~ Soumen

      Comment

      • Juha Nieminen

        #4
        Re: Call by value vs. Call by reference

        Saeed Amrollahi wrote:
        I have learned when an object is big, I should pass the object using a
        reference or pointer to it rather than calling by value.
        In the following code, I don't gain cosiderable performance with call-
        by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
        unconsiderable:
        How do you measure the speed of the program? Maybe copying that vector
        is relatively so fast that you don't notice the difference. Try calling
        each function in a loop to see if there's a difference.

        Comment

        • acehreli@gmail.com

          #5
          Re: Call by value vs. Call by reference

          On Jul 11, 7:08 am, Soumen <soume...@gmail .comwrote:
          On Jul 11, 1:37 pm, Saeed Amrollahi <s_amroll...@ya hoo.comwrote:
          void f1(std::vector< intv)
          {
                  cout << "f1 called" << '\n';
                  for (std::vector<in t>::size_type sz = 0; sz < v.size(); sz++)
                          v[sz] = sz;
          That is not "inserting" an element, rather assigning to an existing
          one.
                  cout << "f1 end" << '\n';
          >
          }
                  vector<intv(500 00000);
          That is a vector with 50 million ints, not empty.
          Probably because your vector is empty (may be it's just holding a
          pointer and few other int)and you're inserting elements inside the
          function.
          That would be this code:

          vector<intv; // empty
          /* ... */
          v.push_back(sz) ; // insert

          Ali

          Comment

          • Thomas J. Gritzan

            #6
            Re: Call by value vs. Call by reference

            Saeed Amrollahi schrieb:
            Dear All
            Hi
            >
            I have learned when an object is big, I should pass the object using a
            reference or pointer to it rather than calling by value.
            In the following code, I don't gain cosiderable performance with call-
            by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
            unconsiderable:
            >
            [...]
            void f1(std::vector< intv)
            [...]
            void f2(std::vector< intv)
            [...]
            void f3(std::vector< int>* pv)
            [...]

            There is no reference. You have two function that do a call-by-value and
            one that does a call-by-reference (via pointer).

            Obviously, the first two shouldn't show any measurable performance
            difference.

            --
            Thomas

            Comment

            • Saeed Amrollahi

              #7
              Re: Call by value vs. Call by reference

              On Jul 11, 9:54 pm, "Thomas J. Gritzan" <phygon_antis.. .@gmx.de>
              wrote:
              Saeed Amrollahi schrieb:Dear All
              Hi
              >
              I have learned when an object is big, I should pass the object using a
              reference or pointer to it rather than calling by value.
              In the following code, I don't gain cosiderable performance with call-
              by-reference vs. call-by-value. Indeed, the perforamnce is absolutely
              unconsiderable:
              >
              [...]
              void f1(std::vector< intv)
              [...]
              void f2(std::vector< intv)
              [...]
              void f3(std::vector< int>* pv)
              >
              [...]
              >
              There is no reference. You have two function that do a call-by-value and
              one that does a call-by-reference (via pointer).
              >
              Obviously, the first two shouldn't show any measurable performance
              difference.
              >
              --
              Thomas
              Hi
              Sorry for typo and confusion. I actually used reference in the code:
              void f1(std::vector< intv)
              void f2(std::vector< int>& v)
              void f3(std::vector< int>* pv)

              Based on

              Comment

              • KalleGuld

                #8
                Re: Call by value vs. Call by reference

                >
                        cout << "f1 called" << '\n';
                        for (int i = 0; i < 10; i++) {
                                vector<intv;
                                f1(v);
                        }
                        cout << "f1 end" << '\n';
                        v.clear();
                        cout << "f2 called" << '\n';
                        for (int i = 0; i < 10; i++) {
                                vector<intv;
                                f2(v);
                        }
                        cout << "f2 end" << '\n';
                        v.clear();
                        cout << "f3 called" << '\n';
                        for (int i = 0; i < 10; i++) {
                                vector<intv;
                                f3(&v);
                        }
                The problem with your benchmark is that most of the work is done
                within the functions themselves.
                I have learned when an object is big, I should pass the object using a
                reference or pointer to it rather than calling by value.
                That's true, but you must understand why. The reason is that when
                calling by value, you make a copy of the object, and copying that
                object takes time.
                But in your example, you just pass an empty vector, and that's quite
                quick to copy.
                Furthermore, it's only _starting_ the function that suffers when
                calling by value, not the work done _inside_ it.

                So, to make your benchmark work better, strip the body of your
                functions and call your functions more times.
                The code below should show the difference quite clearly (part 1 took
                about 3 minutes, and part 2 was under a second on my system).


                //speedtest.cpp
                #include <vector>

                #include <iostream>
                using namespace std;


                double f1(vector<doubl ein) {
                return in[0];
                }
                double f2(vector<doubl e&in) {
                return in[1];
                }
                int main() {

                vector<doubleve c(1000000); //big vector
                vec[0]=1.2;
                vec[1]=3.4;
                double db;
                int bigNum = 10000;
                cout << "start f1:\n" << endl; //remember to flush...
                for (int i=0;i< bigNum ;i++) {
                db = f1(vec);
                }
                cout << "start f2:\n" << endl;
                for (int i=0;i< bigNum ;i++) {
                db = f2(vec);
                }
                cout << "end" << endl;

                return 0;
                }

                Comment

                Working...