On Jul 21, 5:28 am, krisp...@[EMAIL PROTECTED]
wrote:
> // helper: accessing vector
> void TestVecSpeedHelper(vector<int> &v)
> {
> v[0] = 3;
> int a = 0;
> for (int iter = 0; iter < MAX_ITER; ++iter)
> for (size_t i = 1; i < SIZE; ++i)
> {
> v[i] = v[i-1] + 1;
> a = v[i];
> }
> cout << "Exiting TestVecSpeedHelper: " << a << endl;}
Since you're testing specifically vector speed, why not a vector
iterator instead of an int?
Change
> for (int iter = 0; iter < MAX_ITER; ++iter)
to
> for (vector<int>::iterator iter = v.begin(),
vector<int>::iterator e = v.end(); iter != e; ++iter)
When I was first learning the STL, my C++ adviser told me, after I
complained about the iterator system of C++--wich I now think is
beautiful, that they probably want you to use iterators because they
abstract from what actually gets done and they probably mask some
unknown inefficiency. Maybe he was on to something.
> One explanation I could think of is, vector, being allocated
> elsewhere
> is losing out on cache hits, causing more page faults.
Well, the int array is allocated to the stack, and the vector, I've
always assumed, is allocated to the heap, so the test won't be fair
unless you allocate the ints on the heap. It's more realistic for that
number of ints, anyway.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|