help-gplusplus
[Top][All Lists]
Advanced

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

stl vector slower than pointer access


From: ufnoise
Subject: stl vector slower than pointer access
Date: 28 Apr 2006 11:21:01 -0700
User-agent: G2/0.2

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.

I find this result suprising for such a simple data type.  Is there any
way to optimize this for vector access?

Regards,

Juan



reply via email to

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