4 ms·
They compile down to the same code on a simple example in release. Post increment: https://godbolt.org/g/k5rcPA https://godbolt.org/g/k5rcPA Pre increment: ht
by santaclaus 10y ago
They compile down to the same code on a simple example in release.
Post increment: https://godbolt.org/g/k5rcPA https://godbolt.org/g/k5rcPA
Pre increment: https://godbolt.org/g/WgfMzU https://godbolt.org/g/WgfMzU
- exDM69 10y agoFor std::vector, they should compile to the same thing, the iterator is just a pointer to a flat array. For std::map (ie. red-black tree) or something more complex, it might make a bigger difference. But even then, the iterator is quite light weight (just a pointer) so copying it is not a big deal.
- nandno 10y agoEven for std::map gcc generates identical code: https://godbolt.org/g/C6kooc https://godbolt.org/g/C6kooc https://godbolt.org/g/7RS5PQ https://godbolt.org/g/7RS5PQ godbolt is a great resource to see just how good modern compilers are.
- SamReidHughes 10y agoI wouldn't say "even" -- that's just another type where the iterator is a trivially copyable/destructable object.