Testing the performance of a loop for a vector of unique_ptr I wrote this simple program that tests how long it takes for a loop to iterate through a vector&lt;unique_ptr&lt;&gt;&gt;. I'm wondering if this a good way to perform such a speed test? Are there better methods to this?

Also, I can't seem to use myVector.max_size()(which is 1073741823) to allocate the maximum amount of entries for a vector. My program crashes even if I go above my current amount of 70 million.

Results:



&gt; Basic took 0ms 
&gt; BasicR took 0ms 
&gt; Range took 0ms 
&gt; Iterator took 0ms
&gt; IteratorR took 36ms


Program:



// main.cpp 
#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;vector&gt;
#include &lt;chrono&gt;
#include &lt;iterator&gt;

namespace LoopType {
    enum Enum {
        Range,
        Basic,
        BasicR,
        Iterator,
        IteratorR
    };
}
class Test {
public:
    explicit Test(const int a) { mA = a; }
    int getNum() { return mA; }
private:
    int mA;
};

typedef std::vector&lt;std::unique_ptr&lt;Test&gt;&gt; TestVector;

// Loop Types
void RangeBasedForLoop(TestVector&amp; vec) {
    for (auto&amp; test : vec) {
        int num = test-&gt;getNum();
    }
}
void BasicLoop(TestVector&amp; vec) {
    for (unsigned i = 0; i &lt; vec.size(); ++i) {
        int num = vec[i]-&gt;getNum();
    }
}
void BasicLoopR(TestVector&amp; vec) {
    for (unsigned i = vec.size() - 1; i &gt; 0; --i) {
        int num = vec[i]-&gt;getNum();
    }
}
void IteratorLoop(TestVector&amp; vec) {
    TestVector::iterator it;
    for (it = vec.begin(); it != vec.end(); ++it) {
        (*it)-&gt;getNum();
    }
}
void IteratorLoopR(TestVector&amp; vec) {
    TestVector::reverse_iterator it;
    for (it = vec.rbegin(); it != vec.rend(); ++it) {
        (*it)-&gt;getNum();
    }
}

// Testing &amp; Printing
long long TestLoop(TestVector&amp; vec, void(*loop)(TestVector&amp;)) {
    auto t1 = std::chrono::high_resolution_clock::now();
    loop(vec);
    auto t2 = std::chrono::high_resolution_clock::now();
    return std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(t2 - t1).count();
}
void PrintLoopTime(LoopType::Enum type, TestVector&amp; vec) {
    switch (type) {
    case LoopType::Basic:
        std::cout &lt;&lt; "Basic took " &lt;&lt; TestLoop(vec, &amp;BasicLoop) &lt;&lt; "ms" &lt;&lt; std::endl;
        break;

    case LoopType::BasicR:
        std::cout &lt;&lt; "BasicR took " &lt;&lt; TestLoop(vec, &amp;BasicLoopR) &lt;&lt; "ms" &lt;&lt; std::endl;
        break;

    case LoopType::Range:
        std::cout &lt;&lt; "Range took " &lt;&lt; TestLoop(vec, &amp;RangeBasedForLoop) &lt;&lt; "ms" &lt;&lt; std::endl;
        break;

    case LoopType::Iterator:
        std::cout &lt;&lt; "Iterator took " &lt;&lt; TestLoop(vec, &amp;IteratorLoop) &lt;&lt; "ms" &lt;&lt; std::endl;
        break;

    case LoopType::IteratorR:
        std::cout &lt;&lt; "IteratorR took " &lt;&lt; TestLoop(vec, &amp;IteratorLoopR) &lt;&lt; "ms" &lt;&lt; std::endl;
        break;
    }
}

int main() {
    const unsigned long seventyMillion = 70000000UL;
    std::cout &lt;&lt; "Creating vector..." &lt;&lt; std::endl;
    std::vector&lt;std::unique_ptr&lt;Test&gt;&gt; testVector;
    testVector.reserve(seventyMillion);
    for (unsigned long i = 0; i &lt; seventyMillion; ++i) {
        testVector.emplace_back(std::make_unique&lt;Test&gt;(i));
    }
    std::cout &lt;&lt; testVector.size() &lt;&lt; " Elements" &lt;&lt; std::endl &lt;&lt; std::endl;
    std::cout &lt;&lt; "Testing loops..." &lt;&lt; std::endl &lt;&lt; std::endl;

    PrintLoopTime(LoopType::Basic, testVector);
    PrintLoopTime(LoopType::BasicR, testVector);
    PrintLoopTime(LoopType::Range, testVector);
    PrintLoopTime(LoopType::Iterator, testVector);
    PrintLoopTime(LoopType::IteratorR, testVector);

    std::cout &lt;&lt; std::endl &lt;&lt; "Any key to exit...";
    std::cin.get();
    return EXIT_SUCCESS;
}
Most of your loops are executing in no time at all because the optimizer is eliminating them since they have no side effects.  You need to do something observable with them, like summing all elements and returning that sum from the function.  Declare a new variable int sum = 0 before the loops, replace the int num = lines with sum +=, then return sum; at the end of the function.  This will require changing the return type for all of them to int, and changing the type of pointer passed to TestLoop.

std::vector::max_size returns a theoretical maximum, not one determined on your current system specs.  In your case, it returns 0x3FFFFFFF, which is the maximum number of 32 bit pointers that can be stored in a 32 bit address program.  This max size also doesn't consider any additional storage space required by objects stored within the vector.  Every element you have is a unique_ptr, which when allocated will take 4 bytes for the int, plus additional overhead for the memory management system (so at least 12 or more likely 16 bytes).

Your program crashes with more then 70 million elements because it runs out of memory.