Boost logo

Boost Users :

From: Janos Vegh (vegh.janos_at_[hidden])
Date: 2006-08-26 13:19:11


Hi,

I was interested in the efficiacy of std::vector.
Using the test program below, I receive the following results.
The "simple" handling is equivalent with the C array access.
The [] and iterator access time of the vector element
is an order of magnitude worse, than that of the simple access.
This is not because of index limit control, it is done by at()
and its use increases that access time by a factor of three.
What is going on in the background here, that the implementation
is so ineffective?
( I did my tests with WinXP/cygwin/g++/time,
using different setting of the #define-s at the beginning)
Shall I expect similar ineffectivity with the other STL objects?

Regards

Janos

Empty cycle
real 0m2.173s
user 0m1.892s
sys 0m0.080s

STL=1, SIMPLE=1, size=0
real 0m4.917s
user 0m4.646s
sys 0m0.070s

STL=1, size=0
real 0m33.939s
user 0m33.658s
sys 0m0.090s

STL=1, ITERATOR=1, size=1000
real 0m34.059s
user 0m33.688s
sys 0m0.100s

STL=1, AT=1, size=1000
real 1m22.769s
user 1m22.368s
sys 0m0.090s

#define STL 1
#define SIMPLE 0
#define ITERATOR 1
#define AT 0
#define EMPTY 1
#if STL
    #include <vector>
#endif
    using namespace std;
#include <iostream>
#define ARR_SIZE 1000
#define CYCLES 1000000
main (int argc, char** argv)
{
    long int i,j,*P;
#if EMPTY
    cout << "Empty cycle";
#else
#if STL
    #if ITERATOR || AT
        std::vector<long int>::iterator pos;
        std::vector<long int> arr(ARR_SIZE);
    #else
        std::vector <long int> arr;
        arr.reserve(ARR_SIZE);
    #endif

    cout << "STL=1";
    #if SIMPLE
    cout << ", SIMPLE=1";
    #endif
    #if ITERATOR
    cout << ", ITERATOR=1";
    #endif
    #if AT
    cout << ", AT=1";
    #endif
    cout << ", size=" << arr.size();
#else
    long int *arr;
    arr = new long int[ARR_SIZE];
    cout << "STL=0";
#endif
#endif
    cout << endl;
    #if SIMPLE && STL
        P = &arr[0];
    #endif
    for(j=0;j<CYCLES; j++)
    {
        #if STL && ITERATOR && !EMPTY
        i = 0;
        for(pos=arr.begin(); pos < arr.end(); ++pos)
        {
            *pos = i;
        }
        #else
        for(i=0; i<ARR_SIZE; i++)
        {
            #if !EMPTY
                #if SIMPLE && STL
                P[i] = i;
                #elif STL && AT
                arr.at(i) = i;
                #else
                arr[i] = i;
                #endif
            #endif
        }
        #endif
    }
    exit(0);
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net