D
Dave Theese
Hello all,
I'm in a poition of trying to justify use of the STL from a performance
perspective. As a starting point, can anyone cite any benchmarks comparing
vectors to plain old statically-declared arrays? I'll also be looking at
the other STL containers later...
Also, I'd appreciate any comments anyone has on the suitability of the
little program below for comparing vectors and arrays. One obvious
shortcoming is that it uses array / vector indexing operations only as
lvalues, not as rvalues...
With VC++ 6.0 on a 1 GHz machine, this program produced the following
interesting results:
Release mode
---------------
Array: 109 seconds
Vector: 109 seconds
Debug mode
-------------
Array: 141 seconds
Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance
perspective, by a factor of 11.75!
The identical release mode results are, I suppose, not surprising.
I'd love to hear people's thoughts or references to benchmarks more
scientific and appropriate than this little test!
Thanks,
Dave
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
namespace
{
const int NUM_ELEMENTS = 100000;
const int NUM_ITERS = 100000;
};
int main()
{
int a[NUM_ELEMENTS];
time_t finish;
int i;
int j;
int k = NUM_ELEMENTS / 2; // An arbitrary element to look up
time_t start;
vector<int> v(NUM_ELEMENTS);
// Test the array
start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
a[j] = i + j;
}
// Try to inhibit any optimizations of the above loops.
++k;
cout << a[k] << endl;
finish = time(NULL);
cout << "Array implementation: " << difftime(finish, start) << endl;
// Test the vector
start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
v[j] = i + j;
}
// Try to inhibit any optimizations of the above loops.
++k;
cout << v[k] << endl;
finish = time(NULL);
cout << "vector implementation: " << difftime(finish, start) << endl;
return 0;
}
I'm in a poition of trying to justify use of the STL from a performance
perspective. As a starting point, can anyone cite any benchmarks comparing
vectors to plain old statically-declared arrays? I'll also be looking at
the other STL containers later...
Also, I'd appreciate any comments anyone has on the suitability of the
little program below for comparing vectors and arrays. One obvious
shortcoming is that it uses array / vector indexing operations only as
lvalues, not as rvalues...
With VC++ 6.0 on a 1 GHz machine, this program produced the following
interesting results:
Release mode
---------------
Array: 109 seconds
Vector: 109 seconds
Debug mode
-------------
Array: 141 seconds
Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance
perspective, by a factor of 11.75!
The identical release mode results are, I suppose, not surprising.
I'd love to hear people's thoughts or references to benchmarks more
scientific and appropriate than this little test!
Thanks,
Dave
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
namespace
{
const int NUM_ELEMENTS = 100000;
const int NUM_ITERS = 100000;
};
int main()
{
int a[NUM_ELEMENTS];
time_t finish;
int i;
int j;
int k = NUM_ELEMENTS / 2; // An arbitrary element to look up
time_t start;
vector<int> v(NUM_ELEMENTS);
// Test the array
start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
a[j] = i + j;
}
// Try to inhibit any optimizations of the above loops.
++k;
cout << a[k] << endl;
finish = time(NULL);
cout << "Array implementation: " << difftime(finish, start) << endl;
// Test the vector
start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
v[j] = i + j;
}
// Try to inhibit any optimizations of the above loops.
++k;
cout << v[k] << endl;
finish = time(NULL);
cout << "vector implementation: " << difftime(finish, start) << endl;
return 0;
}