CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Oct 2017
    Posts
    50

    [SOLVED]std::vector::emplace_back vs std::vector::push_back

    Well i just figured out there's a emplace_back method than does same thing as push_back after 4 years messing around with c++. Which one is faster or which one should I use. Currently i use emplace_back like this to retrive vertices data from aiScene of Assimp to my verticeData vector like this.
    Code:
    for (uint32_t numOfVertexComponet = 0; numOfVertexComponet < vertexComponent.size(); ++numOfVertexComponet)
    			{
    				if (vertexComponent[numOfVertexComponet] == VERTEX_COMPONENT_POSITION)
    				{
    					aiVector3D position = mesh->mVertices[numOfVertices];
    
    					verticesData.emplace_back(position.x);
    					verticesData.emplace_back(position.y);
    					verticesData.emplace_back(position.z);
    				}
    				else if (vertexComponent[numOfVertexComponet] == VERTEX_COMPONENT_NORMAL)
    				{
    					aiVector3D normal = mesh->mNormals[numOfVertices];
    
    					verticesData.emplace_back(normal.x);
    					verticesData.emplace_back(normal.y);
    					verticesData.emplace_back(normal.z);
    				}
    			}
    Last edited by noobofcpp; April 16th, 2021 at 04:28 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: std::vector::emplace_back vs std::vector::push_back

    Quote Originally Posted by noobofcpp View Post
    Well i just figured out there's a emplace_back method than does same thing as push_back after 4 years messing around with c++. Which one is faster or which one should I use.
    Did you try to measure the execution times of both emplace_back and push_back methods and compare these?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2017
    Posts
    50

    Re: std::vector::emplace_back vs std::vector::push_back

    after you asked to measure execution time, i did test with std::chrono::steady_clock::now() on it and emplace_back is slightly faster. Sorry didnt thought to test it before. So the emplace_back is better than push_back?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: std::vector::emplace_back vs std::vector::push_back

    Quote Originally Posted by noobofcpp View Post
    after you asked to measure execution time, i did test with std::chrono::steady_clock::now() on it and emplace_back is slightly faster. Sorry didnt thought to test it before. So the emplace_back is better than push_back?
    I don't know which one is faster cause I never used emplace_back and never had any lengthy calculations with vectors.

    Note also that you should perform your tests with release builds!
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: std::vector::emplace_back vs std::vector::push_back

    emplace back enables the arguments of a class/struct constructor to be specified without the requirement to create another instance of the class.

    As a simple example, consider:

    Code:
    #include <vector>
    
    struct mytype {
    	int a {};
    	int b {};
    
    	mytype() {}
    	mytype(int aa, int bb) : a(aa), b(bb) {}
    };
    
    int main()
    {
    	std::vector<mytype> myvec;
    
    	myvec.emplace_back(4, 5);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: std::vector::emplace_back vs std::vector::push_back

    Consider this test class and the generated output from various uses of puish_back() and emplace_back():

    Code:
    include <vector>
    #include <iostream>
    
    struct mytype {
    	int a {};
    	int b {};
    
    	mytype() { std::cout << "def cons\n"; }
    	mytype(int aa, int bb) : a(aa), b(bb) { std::cout << "const " << aa << " " << bb << '\n'; }
    	mytype(const mytype& t1) : a(t1.a), b(t1.b) { std::cout << "copy const " << t1.a << " " << t1.b << '\n'; }
    	mytype& operator=(const mytype& t1) { a = t1.a; b = t1.b; std::cout << "copy oper= " << t1.a << " " << t1.b << "\n"; return *this; }
    
    	~mytype() { std::cout << "deconst\n"; }
    };
    
    int main()
    {
    	std::vector<mytype> myvec;
    	myvec.reserve(10);
    
    	mytype m1 {6, 7};
    
    	std::cout << "\npushback 4 5\n";
    	myvec.push_back(mytype {4, 5});
    
    	std::cout << "\npushback m1\n";
    	myvec.push_back(m1);
    
    	std::cout << "\nemplaceback 4 5\n";
    	myvec.emplace_back(4, 5);
    
    	std::cout << "\nemplace back m1\n";
    	myvec.emplace_back(m1);
    
    	std::cout << "\nfinished\n";
    }
    Code:
    const 6 7
    
    pushback 4 5
    const 4 5
    copy const 4 5
    deconst
    
    pushback m1
    copy const 6 7
    
    emplaceback 4 5
    const 4 5
    
    emplace back m1
    copy const 6 7
    
    finished
    deconst
    deconst
    deconst
    deconst
    deconst
    Last edited by 2kaud; April 15th, 2021 at 05:04 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Oct 2017
    Posts
    50

    Re: std::vector::emplace_back vs std::vector::push_back

    thanks for the explaination sir. i think i understand

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured