[PATCH] D35145: Use emplace_back to replace size() and resize().

Dehao Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 10 08:31:42 PDT 2017


danielcdh added a comment.

In https://reviews.llvm.org/D35145#803115, @tejohnson wrote:

> LGTM
>
> In https://reviews.llvm.org/D35145#802899, @grandinj wrote:
>
> > I'm curious if you know why emplace_back is so much faster than resize(size+1) ?
>
>
> My understanding is that resize copies in the default value whereas emplace_back constructs one in place, and the latter is more efficient (especially when only 1 element being added to the list). Dehao - is that the reasoning you had?


To calculate size(), one needs to traverse the entire list, which is O(n). resize also need to traverse to the end of the list and append on the tail.


https://reviews.llvm.org/D35145





More information about the llvm-commits mailing list