C++ Array vs Vector performance test explanation -
in order quantify difference in performance of c-like array , vectors in c++, wrote little program. https://github.com/rajatkhanduja/benchmarks/blob/master/c%2b%2b/vectorvsarray.cpp
to compare them on common grounds, decided test both random , sequential access. added iterators, compare them (but not question focusses on).
the results, 64-bit linux machine 7.7 gb ram on array/vector size of 1 million follows:-
- time taken write array. : 12.0378 ms
- time taken read array sequentially. : 2.48413 ms
- time taken read array randomly. : 37.3931 ms
- time taken write dynamic array. : 11.7458 ms
- time taken read dynamic array sequentially. : 2.85107 ms
- time taken read dynamic array randomly. : 36.0579 ms
- time taken write vector using indices. : 11.3909 ms
- time taken read vector using indices, sequentially. : 4.09106 ms
- time taken read vector using indices, randomly. : 39 ms
- time taken write vector using iterators. : 24.9949 ms
- time taken read vector using iterators. : 18.8049 ms
the vector's size set @ time of initialization , not altered, there no resizing of vector (the assertion in program helps verify that). times don't include initialization times of of statically allocated array, dynamically allocated array or vector.
according statistics, time write vector lesser of array time read vector twice as of array.
the difference pretty small, there explanation why there performance difference ? there wrong test ? expected both perform @ same speed. repetition of test shows same trend.
the code:
#include <vector> #include <iostream> #include <cstdlib> #include <ctime> #include <sys/time.h> #include <cassert> #define arr_size 1000000 using std::string; void printtime (struct timeval& start, struct timeval& end, string str); int main (void) { int arr[arr_size]; int tmp; struct timeval start, stop; srand (time (null)); /* writing data array */ gettimeofday (&start, null); (int = 0; < arr_size; i++) { arr[i] = rand(); } gettimeofday (&stop, null); printtime (start, stop, string ("time taken write array.")); /* reading data array */ gettimeofday (&start, null); (int = 0; < arr_size; i++) { tmp = arr[i]; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read array sequentially.")); /* reading data array randomly*/ gettimeofday (&start, null); (int = 0; < arr_size; i++) { tmp = arr[rand() % arr_size]; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read array randomly.")); int *darr = (int *) calloc (sizeof (int), arr_size); /* writing data array */ gettimeofday (&start, null); (int = 0; < arr_size; i++) { darr[i] = rand(); } gettimeofday (&stop, null); printtime (start, stop, string ("time taken write dynamic array.")); /* reading data array */ gettimeofday (&start, null); (int = 0; < arr_size; i++) { tmp = darr[i]; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read dynamic array sequentially.")); /* reading data dynamic array randomly*/ gettimeofday (&start, null); (int = 0; < arr_size; i++) { tmp = darr[rand() % arr_size]; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read dynamic array randomly.")); std::vector<int> v(arr_size); assert (v.capacity() == arr_size); /* writing vector using indices*/ gettimeofday (&start, null); (int = 0; < arr_size; i++) { v[i] = rand(); } gettimeofday (&stop, null); printtime (start, stop, string ("time taken write vector using indices.")); assert (v.capacity() == arr_size); /* reading vector using indices*/ gettimeofday (&start, null); (int = 0; < arr_size; i++) { tmp = v[i]; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read vector using indices, sequentially.")); /* reading data dynamic array randomly*/ gettimeofday (&start, null); (int = 0; < arr_size; i++) { tmp = v[rand() % arr_size]; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read vector using indices, randomly.")); std::vector<int> v2(arr_size); /* writing vector using iterators*/ gettimeofday (&start, null); std::vector<int>::iterator itr, itr_end; (itr = v2.begin(), itr_end = v2.end(); itr != itr_end; itr++) { *itr = rand(); } gettimeofday (&stop, null); printtime (start, stop, string ("time taken write vector using iterators.")); /* reading vector using iterators*/ gettimeofday (&start, null); (itr = v2.begin(), itr_end = v2.end(); itr != itr_end; itr++) { tmp = *itr; } gettimeofday (&stop, null); printtime (start, stop, string ("time taken read vector using iterators.")); return 0; } void printtime (struct timeval& start, struct timeval& end, string str) { double start_time, end_time, diff; start_time = ((start.tv_sec) * 1000 + start.tv_usec/1000.0); end_time = ((end.tv_sec) * 1000 + end.tv_usec/1000.0); diff = end_time - start_time; std::cout << str << " : " << diff << " ms" << std::endl; } edit
as suggested in comments, here more information :-
- compiler :- g++ - 4.5.2
- flags :- none (i.e. default values)
- optimizations :- none (i wanted test behavior in usual setting. optimization vary behavior of program, instance since variable tmp never used, step of reading vector/array may altogether skipped or reduced last assignment. @ least that's understand).
certainly not definitive answer, writing in loop variable, meaning compiler can guess end result should sequential reading, optimizing loop away. since it's not doing this, assume there no optimization doesn't favor iterator approach. other numbers close take conclusions.
Comments
Post a Comment