Thread: c++ efficiency subject

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    13

    c++ efficiency subject

    Hi there!


    This is a subject related to copy constructors and efficiency in C++. The following code snippet shows three functions: 'example1', 'example2' and 'example3'. I have been discussing with a workmate which one is the most efficient :


    Code:
    
    #include <iostream>
    #include <string>
    
    
    //just an example class
    class MyClass
    {
    public:
        std::string toString()
        {
            return "just an example";
        }
    };
    
    
    static MyClass myObj;
    
    
    
    
    bool example1()
    {
        std::string str = myObj.toString();//executing copy constructor of std::string
        
        //... some code
        std::cout << str << std::endl;
        
        //.. some other code
        std::cout << str << std::endl;
        
        return true;
    }
    
    
    bool example2()
    {
        const std::string& str = myObj.toString();//avoid copy constructor of std::string, but more verbose.
        
        //... some code
        std::cout << str << std::endl;
        
        //.. some other code
        std::cout << str << std::endl;
        
        return true;
    }
    
    
    bool example3()
    {
        //avoid copy constructor of std::string, but we get two call stacks .
        
        //... some code
        std::cout << myObj.toString() << std::endl;
        
        //.. some other code
        std::cout << myObj.toString() << std::endl;
        
        return true;
    }
    
    
    
    
    int main()
    {
        for (size_t i = 0; i < 1000000; i++)
        {
            //example1();
            example2();
            //example3();
        }
    }

    In my opinion, the less efficient one is 'example1' because it implies the execution of the copy constructor which, in turn, implies a system call to allocate memory and another call to free such memory. But my workmate says 'example1' is the most efficient one, because the compiler will perform an optimization to avoid the copy constructor.


    What is your opinion about that?


    My opinion is that efficiency issues should not be left to the compiler, they should always be fixed in the c++ code. The code is aimed to be run on all platforms (Windows, Linux and Mac) and different compilers may behave in a different way. Even different versions of the same compiler may or may not perform optimizations.




    I would choose 'example2' or even 'example3' because a call stack will always be faster than a system call to allocate memory.


    Here I used std::string as an example, but it can be with any user defined complex type.


    Thanks a lot!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also here.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hebrerillo
    My opinion is that efficiency issues should not be left to the compiler, they should always be fixed in the c++ code. The code is aimed to be run on all platforms (Windows, Linux and Mac) and different compilers may behave in a different way. Even different versions of the same compiler may or may not perform optimizations.
    We're talking about return value optimisation, which is explicitly permitted by the standard and so common that it is inconceivable that it would not be applied. It would be a bit like arguing that you need to explicitly compute a constant yourself from a calculation with only built-in operators involving constants instead of reasonably assuming that the compiler will perform constant folding.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About NULL subject in C
    By RyanC in forum C Programming
    Replies: 4
    Last Post: 12-03-2017, 01:18 PM
  2. Subject:Win'98 Desktop.
    By Adock in forum Tech Board
    Replies: 2
    Last Post: 09-17-2007, 01:11 PM
  3. Good Subject!
    By Melissa Atkin in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-27-2002, 04:09 PM
  4. Which subject????
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-20-2002, 11:06 AM
  5. Subject Descriptions
    By tim545666 in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 04:59 AM

Tags for this Thread