Advertisement
Guest User

timingargmax.cpp

a guest
Jun 14th, 2011
1,023
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <limits.h>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <sys/stat.h>
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8.  
  9.  
  10. class ZTimer
  11. {
  12. public:
  13.     struct timeval t1, t2;
  14. public:
  15.     ZTimer() :  t1(), t2() { gettimeofday(&t1,0); t2 = t1; }
  16.     void reset() {gettimeofday(&t1,0); t2 = t1;}
  17.     int elapsed() { return ((t2.tv_sec - t1.tv_sec) * 1000) + ((t2.tv_usec - t1.
  18. tv_usec) / 1000); }
  19.     int split() { gettimeofday(&t2,0); return elapsed(); }
  20. };
  21. using namespace std;
  22. int main() {
  23.   const int N = 100 * 1000 * 1000;
  24.   vector<int> array;
  25.   array.resize(N);
  26.   for(int k = 0; k<N;++k)
  27.     array[k]=k;
  28.   random_shuffle(array.begin(), array.end());
  29.   ZTimer z;
  30.   for(int j = 0; j<10;++j) {
  31.       int max = INT_MIN;
  32.       int bestk = 0;
  33.       for (int k = 0; k<N; ++k) {
  34.         if(array[k]>max) {
  35.             max = array[k];
  36.             bestk = k;
  37.         }
  38.       }
  39.       cout<<" "<< bestk<<endl;
  40.   }
  41.   cout<<z.split()<<endl;
  42.   z.reset();
  43.   for(int j = 0; j<10;++j) {
  44.       int max = INT_MIN;
  45.       for (int k = 0; k<N; ++k) {
  46.         if(array[k]>max) max = array[k];
  47.       }
  48.       for (int k = 0; k<N;++k)
  49.        if(array[k] == max) {
  50.             cout<<" "<< k<<endl;
  51.             break;
  52.        }
  53.   }
  54.   cout<<z.split()<<endl;
  55.   return 0;
  56. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement