2

If I have std::vector (which is a std::vector and will be always a std::vector).

Is it superior to use std::begin() instead of std::vector::begin() (or the opposite)?

Will there be any performance increase/decrease?

Example:

std::vector<int> foo(100, 5);
std::sort(foo.begin(), foo.end());        // Case 1
std::sort(std:begin(foo), std::end(foo)); // Case 2
Patryk
  • 22,602
  • 44
  • 128
  • 244
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 2
    Duplicate of [Difference between vector::begin() and std::begin()](http://stackoverflow.com/questions/26290316/difference-between-vectorbegin-and-stdbegin). The first answer seems to answer your question. – Nathan Cooper Feb 16 '16 at 11:41
  • 1
    The duplicate is all it's about. The non-member function will call the member one and it will be perfectly inlined. – LogicStuff Feb 16 '16 at 11:41
  • 1
    `std::begin` will also work with C style arrays, which is the purpose of the difference. As far as style goes, my brain prefers objects owning their own behaviour, when you don't have to use C style arrays that is. – Nathan Cooper Feb 16 '16 at 11:44
  • I saw the duplicate before posting. I do not think there is anything about performance there. – Humam Helfawi Feb 16 '16 at 11:44
  • @Tartan Unduplicated: in order to find a duplicate, you need to find an answer that actually addresses the performance parts of the above question. – Yakk - Adam Nevraumont Feb 16 '16 at 14:15

2 Answers2

4

For "normal" std-container types std::begin(c) is actually the same as c.begin().

Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
3

My two pence:

(which is a std::vector and will be always a std::vector).

IMHO this is a guarantee that you cannot make now - this argues for the free function form.

Is it superior to use std::begin() instead of std::vector::begin() (or the opposite)?

Only in the sense that the free function form participates in ADL

Will there be any performance increase/decrease?

Not if you enable the optimiser.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Thanks... "Not if you enable the optimiser." May you please go a little deeper here if you do not mind. – Humam Helfawi Feb 16 '16 at 11:46
  • If you don't turn on optimizations, the compiler will generate code that actually calls std::begin(), which in turn directly calls std::vector::begin(). If you have optimizations turned on, it will effectively skip the std::begin() call and go straight to std::vector::begin(), if it even does that. Quite possibly it will inline the guts of std::vector::begin() directly at the call site. – Rob K Feb 16 '16 at 16:10
  • @HumamHelfawi Rob's answer here is correct. I would go further since I have looked at the assembly output of using std::algorithms and containers under clang/libc++ with -O2/3. The compiler boils it all down to pointer-based iteration, creating almost perfectly optimal code. – Richard Hodges Feb 16 '16 at 17:40
  • @RobK I would have agreed with you, but at least gcc with -O0 generates exactly the same code for `std::vector`: http://goo.gl/Mm8tDg vs http://goo.gl/3UM3r2 – Simon Kraemer Mar 03 '16 at 08:43