Running it many times, it outputs times similar to:
-----Iteration: 0------------------
Time for vector iterator : 0.2557
Time for vector int count: 0.1166
Time for array : 0.0232
-----Iteration: 1------------------
Time for vector iterator : 0.2443
Time for vector int count: 0.1159
Time for array : 0.0236
-----Iteration: 2------------------
Time for vector iterator : 0.244
Time for vector int count: 0.1154
Time for array : 0.0233
-----Iteration: 3------------------
Time for vector iterator : 0.2436
Time for vector int count: 0.1154
Time for array : 0.0249
-----Iteration: 4------------------
Time for vector iterator : 0.2436
Time for vector int count: 0.1166
Time for array : 0.0233
-----Iteration: 5------------------
Time for vector iterator : 0.2443
Time for vector int count: 0.1151
Time for array : 0.0232
-----Iteration: 6------------------
Time for vector iterator : 0.2455
Time for vector int count: 0.1158
Time for array : 0.0233
-----Iteration: 7------------------
Time for vector iterator : 0.2491
Time for vector int count: 0.1155
Time for array : 0.0233
-----Iteration: 8------------------
Time for vector iterator : 0.2557
Time for vector int count: 0.1151
Time for array : 0.0236
-----Iteration: 9------------------
Time for vector iterator : 0.2439
Time for vector int count: 0.1183
Time for array : 0.0236
Average over 10 runs -----------------
Vector iterator : 0.24697
Vector int count: 0.11597
Array iteration : 0.02353
Iterating through a vector rather an a raw array takes... 10 times as long?! I knew there would be a time difference, but I don't really understand why it would be so much slower. It seems using a range-based for loop is was the slowest, iterating through using [] was the middle guy, but iterating through a raw array was the fastest, 1/10th the time of using iterators and 1/5th the time of iterating through a vector using [].
I don't think I understand enough about how C++ works under the hood to really understand why there's such a massive time difference. Can anyone perhaps enlighten me? Clearly this doesn't make much of a difference for a small number of elements, but can for a very large number of elements.
Using std::chrono::system_clock instead of std::chrono::high_resolution_clock to only include time spent inside the program's execution.
Your code contains bugs. The loop starting at line 28 doubles the size of the vector, so the ranged for performs twice as much work as the array loop.
Average over 50 runs -----------------
Vector iterator : 6.37506
Vector int count: 6.574
Array iteration : 6.52352
The code generated by ranged for is generally the most optimal because the compiler can make stronger assumptions.
If you are seeing huge performance differences when using std::vector vs. raw arrays, turn on optimizations.
Well, pardon my stupid mistake.
I did still see some big differences after fixing that, but turning on optimizations fixed it. Regular iteration in the array was then the slowest.
My computer gives everything as 0ms, but shows ranged for loop as faster if I slow it down.
Thanks for pointing that out :)
*facepalm*