AngusJohnson / Clipper2

Polygon Clipping and Offsetting - C++, C# and Delphi
Boost Software License 1.0
1.5k stars 273 forks source link

Replace Vector push_back() vs emplace_back() #175

Closed aismann closed 2 years ago

aismann commented 2 years ago

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 commented 2 years ago

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

aismann commented 2 years ago

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

AngusJohnson commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Interesting. Thanks. Can I close this issue?