-
April 14th, 2021, 11:46 PM
#1
[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.
-
April 15th, 2021, 01:13 AM
#2
Re: std::vector::emplace_back vs std::vector::push_back
Originally Posted by noobofcpp
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
-
April 15th, 2021, 01:55 AM
#3
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?
-
April 15th, 2021, 02:55 AM
#4
Re: std::vector::emplace_back vs std::vector::push_back
Originally Posted by noobofcpp
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
-
April 15th, 2021, 03:37 AM
#5
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)
-
April 15th, 2021, 05:00 AM
#6
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)
-
April 16th, 2021, 04:27 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|