Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <ctime>
- #define SIZETOTEST 100000
- int main (int argc, char * const argv[]) {
- std::vector<int> myvec;
- int myarray[SIZETOTEST];
- for (int i = 0; i < SIZETOTEST; ++i) {
- myvec.push_back(i);
- myarray[i] = i;
- }
- typedef std::vector<int>::iterator IT_int;
- clock_t start,end;
- start = std::clock();
- for (IT_int it = myvec.begin(); it != myvec.end(); ++it) {
- *it += 1;
- }
- end = std::clock();
- std::cout << "iterator it: " << end - start << std::endl;
- start = std::clock();
- for (int i = 0; i < myvec.size(); ++i) {
- myvec[i] += 1;
- }
- end = std::clock();
- std::cout << "int i: "<< end - start << std::endl;
- start = std::clock();
- for (int i = 0; i < SIZETOTEST; ++i) {
- myarray[i] = myarray[i] + 1;
- }
- end = std::clock();
- std::cout << "c-style array: " << end - start << std::endl;
- return 0;
- }
- /*
- output: compiled with compiled with XCode 3.2.5, GCC 4.2
- in debug mode (no or really few optimization):
- iterator it: 2430
- int i: 1141
- c-style array: 561
- in release mode:
- iterator it: 108
- int i: 160
- c-style array: 2
- */
Advertisement
Add Comment
Please, Sign In to add comment