4

Is this a fair test for comparing a vector with an array? The difference in speed seems too large. My test suggests the array is 10 to 100 times faster!

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <windows.h>
#include <stdint.h>

using namespace std;

double PCFreq = 0.0;
__int64 CounterStart = 0;

using namespace std;

void StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    std:cout << "QueryPerformanceFrequency failed!\n";

    PCFreq = double(li.QuadPart)/1000000000;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //Can do 100,000 but not 1,000,000
    const int vectorsize = 100000;
    cout.precision(10);

    StartCounter();
    vector<int> test1(vectorsize);
    for(int i=0; i<vectorsize; i++){
        test1[i] = 5;
    }
    cout << GetCounter() << endl << endl;


    StartCounter();
    int test2[vectorsize];
    for(int i=0; i<vectorsize; i++){
        test2[i] = 5;
    }
    cout << GetCounter() << endl << endl;

    cout << test2[0];

    int t = 0;
    cin >> t;
    return 0;
}
11
  • 1
    Look at the assembly code. Chances are, almost everything is optimized out in a release build. Dec 15, 2012 at 18:19
  • 3
    Indeed, it's very likely that your second loop is being completely removed by the compiler.
    – Mysticial
    Dec 15, 2012 at 18:19
  • 2
    @user997112 Correct. It's called Dead Code Elimination. Here's another example: stackoverflow.com/questions/8841865/…
    – Mysticial
    Dec 15, 2012 at 18:22
  • 1
    @user997112: The fact that you see this sort of difference probably indicates that you run some sort of debug build, non-optimized (or even deliberately de-optimized) and heavily overloaded with assertions (iterator checks and stuff like that). It makes absolutely no sense to run any comparisons in debug builds. Dec 15, 2012 at 18:33
  • 3
    One difference is that the vector elements are first initialized to zero in the constructor, and then assigned the value 5. Try vector<int> test1(vectorsize, 5); instead.
    – Bo Persson
    Dec 15, 2012 at 18:35

1 Answer 1

12

It depends on what you are comparing.

Your benchmark measures both setup time and access times together. It's doubtless that std::vector has a more expensive setup time. This is because it needs to allocate memory, and then (by necessity of the standard) call default constructors on all the elements. Which for a POD type, means zeroing.

So if you're trying to measure access times, then no your benchmark isn't accurate.

Here's some numbers to digest:

Original Code:

StartCounter();
vector<int> test1(vectorsize);

for(int i=0; i<vectorsize; i++){
    test1[i] = 5;
}
cout << GetCounter() << endl << endl;

Time: 444353.5206


Start timing after declaring and initializing the vector:

vector<int> test1(vectorsize);

StartCounter();
for(int i=0; i<vectorsize; i++){
    test1[i] = 5;
}
cout << GetCounter() << endl << endl;

Time: 15031.76101


And for the array:

StartCounter();
int test2[vectorsize];
for(int i=0; i<vectorsize; i++){
    test2[i] = 5;
}
cout << GetCounter() << endl << endl;

Time: 38129.345

The times are about the same regardless of whether the declaration is timed. This is likely because stack allocation is done all at once upon entry to the function.


Basically, the vector memory allocation and initialization is taking a disproportionate amount of time. But the actual loop is fast.

I'll also note that your current benchmark framework is still sightly flawed. You only make one pass over each array. So cache-effects and lazy-allocation will be significant.

The reason why the array is now slower is likely due to lazy-allocation. The array is allocated, but it hasn't been committed yet. Lazy allocation means that it is committed upon first access - which involves a page-fault and a context-switch to the kernel.


Here's a fairer test with an outer loop to increase the benchmark time:

vector<int> test1(vectorsize);

StartCounter();
for (int c = 0; c < 10000; c++){
    for(int i=0; i<vectorsize; i++){
        test1[i] = 5;
    }
}
cout << GetCounter() << endl << endl;

Time: 227330454.6

int test2[vectorsize];
memset(test2,0,sizeof(test2));

StartCounter();
for (int c = 0; c < 10000; c++){
    for(int i=0; i<vectorsize; i++){
        test2[i] = 5;
    }
}
cout << GetCounter() << endl << endl;
cout << test2[0];

Time: 212286228.2

So no an array is NOT faster than a vector for steady-state access. It's just tricky to benchmark properly.

18
  • what about starting the array counter after the declaration also? to make both loops start the counter at the same point in time. Dec 15, 2012 at 18:44
  • @Need4Sleep I did it both ways. There was no performance difference. That's because the array is on the stack and is allocated upon entry to the function.
    – Mysticial
    Dec 15, 2012 at 18:45
  • Removing both initializations from the timing, why would the vector then be faster than the static array loop? Im looking at the release assembler and the static array loop only has 2x MOVs, 1x LEA and 1 repo stos instructions?
    – user997112
    Dec 15, 2012 at 18:45
  • @user997112 That's probably because the array probably isn't paged yet due to lazy allocation. So the OS needs to zero it upon committing it due to security reasons.
    – Mysticial
    Dec 15, 2012 at 18:46
  • 1
    @Mystical If you overallocate you might run the kernel zero pool down to empty, at which point you might find your thread descheduled until the pool has replenished. A better test is to try timing a one page alloc versus a one page free followed with a thread sleep between test instances to give the zero pool time to replenish. This behaviour has been the case since at least Windows 2000 and is documented in the Windows Internals series of books. Dec 16, 2012 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.