Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Vector push_back() vs emplace_back() #175

Closed
aismann opened this issue Aug 30, 2022 · 5 comments
Closed

Replace Vector push_back() vs emplace_back() #175

aismann opened this issue Aug 30, 2022 · 5 comments

Comments

@aismann
Copy link
Contributor

aismann commented Aug 30, 2022

push_back and emplace_back in C++
Push_back: Push_back adds a new element at the end. It first creates a temporary object by calling a constructor.

Emplace_back: It also Inserts a new element at the end. It does not create a temporary object. The object is directly created in the vector. Due to this, the efficiency is increased.

The difference in the efficiency of push_back and emplace_back depends on the type of our vector. If the vector is a built-in type, there is no difference between the efficiency of push_back and emplace_back. If the vector type is class or struct, emplace_back is more efficient than push_back.

image

see also:
https://www.codingninjas.com/codestudio/library/vector-push_back-vs-emplace_back#:~:text=Push_back%3A%20Push_back%20adds%20a%20new%20element%20at%20the,vector.%20Due%20to%20this%2C%20the%20efficiency%20is%20increased.

@AngusJohnson
Copy link
Owner

Thank you for your excellent suggestion.
Would you be prepared to make a Pull Request with proposed changes?

aismann added a commit to aismann/Clipper2 that referenced this issue Aug 31, 2022
@aismann
Copy link
Contributor Author

aismann commented Aug 31, 2022

A new performance check will be great (I am very curious about the measurement)

@AngusJohnson
Copy link
Owner

Thanks heaps for the PR, but I want to check it out before committing it.

A new performance check will be great (I am very curious about the measurement)

Indeed, and me too! 😜

@AngusJohnson
Copy link
Owner

I've just run the simple benchmark test that's in ConsoleDemo1 and I'm sorry to say there's been no measurable change in performance 😞. Unless you can demonstrate otherwise, I'm not inclined to change the existing code. Anyhow, I appreciate the feedback and maybe you can find performance improvements somewhere else? 🤞🙏.

@aismann
Copy link
Contributor Author

aismann commented Aug 31, 2022

Interesting. Thanks.
Can I close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants