help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stl vector slower than pointer access


From: Rolf Magnus
Subject: Re: stl vector slower than pointer access
Date: Sat, 29 Apr 2006 17:06:55 +0200

ufnoise@gmail.com wrote:

> Using this simple test program, it seems that using the operator[] for
> vector access is slower than accessing through a double pointer array
> access.
> 
> #include <vector>
> using namespace std;
> int main()
> {
>   vector<double> v(10000);
>   const size_t len = v.size();
>   for (size_t j = 0; j < 100000; ++j)
>   {
>     double *p = &v[0];
>     for (size_t i = 0; i < v.size(); ++i)
>       v[i] = 1;
> //      p[i]=1;
> 
> 
>   }
> 
> }
> 
> Running using v[i] in the code above results in about 2.5 sec run time.
>  Running with p[i] instead of v[i] results in about 1.5 sec run time on
> an amd 64.  This is with gcc version 4.1 and using -O3.

On my x86 system with gcc 4.0.3, they are both the same, so I guess it's
either a problem of the amd64 target or gcc 4.1.

BTW:

When I change the loop into:

    for (vector<double>::iterator it = v.begin(); it != v.end(); ++it)
        *it = 1;

it becomes faster (2.15 seconds versus 2.7 seconds).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]