Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Array vs Ve...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 14 Topic 9810 of 9983
Post > Topic >>

Re: Array vs Vector performance

by "Hakusa@[EMAIL PROTECTED] " <Hakusa@[EMAIL PROTECTED] > Jul 22, 2008 at 03:58 PM

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! ]
 




 14 Posts in Topic:
Array vs Vector performance
krisprad@[EMAIL PROTECTED  2008-07-21 04:28:16 
Re: Array vs Vector performance
"Martin T." <  2008-07-21 14:38:29 
Re: Array vs Vector performance
Mathias Gaunard <loufo  2008-07-21 14:40:18 
Re: Array vs Vector performance
Alberto Ganesh Barbati &l  2008-07-21 15:17:18 
Re: Array vs Vector performance
tni <nobody@[EMAIL PRO  2008-07-21 15:22:02 
Re: Array vs Vector performance
"Hakusa@[EMAIL PROTE  2008-07-22 15:58:15 
Re: Array vs Vector performance
Andre Poenitz <poenitz  2008-07-23 19:30:22 
Re: Array vs Vector performance
JoshuaMaurice@[EMAIL PROT  2008-07-23 19:40:18 
Re: Array vs Vector performance
krisprad@[EMAIL PROTECTED  2008-07-23 19:58:15 
Re: Array vs Vector performance
Francis Glassborow <fr  2008-07-24 05:15:51 
Re: Array vs Vector performance
Alberto Ganesh Barbati &l  2008-07-24 05:13:20 
Re: Array vs Vector performance
Mirco Wahab <wahab@[EM  2008-07-24 13:26:45 
Re: Array vs Vector performance
tni <nobody@[EMAIL PRO  2008-07-24 13:58:16 
Re: Array vs Vector performance
krisprad@[EMAIL PROTECTED  2008-07-24 17:21:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 15:34:47 CDT 2008.