Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Jul 19th, 2010 | Syntax: C++ | Size: 2.90 KB | Hits: 159 | Expires: Never
Copy text to clipboard
  1. #include <sys/stat.h>
  2. #include <sys/time.h>
  3. #include <sys/types.h>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. //g++ -O2 -o sillybenchmark sillybenchmark.cpp
  8.  
  9.  
  10. typedef unsigned long long uint64;
  11. class ZTimer
  12. {
  13. public:
  14.     struct timeval t1, t2;
  15. public:
  16.     ZTimer() { gettimeofday(&t1,0); t2 = t1; }
  17.     void reset() {gettimeofday(&t1,0); t2 = t1;}
  18.     int elapsed() { return ((t2.tv_sec - t1.tv_sec) * 1000) + ((t2.tv_usec - t1.
  19. tv_usec) / 1000); }
  20.     int split() { gettimeofday(&t2,0); return elapsed(); }
  21. };
  22.  
  23.  
  24. uint64 test1() {// standard scalar product
  25.         const int N =1024*32;
  26.         uint64 vector1[N];
  27.         uint64 vector2[N];
  28.         uint64 answer;
  29.         for(int k =0; k<N;++k)
  30.          answer += vector1[k] * vector2[k];
  31.         return answer;
  32. }
  33.  
  34. uint64 test2() {//  scalar product 2x2
  35.         const int N = 1024*32;
  36.         uint64 vector1[N];
  37.         uint64 vector2[N];
  38.         uint64 answer;
  39.         for(int k =0; k+1<N;k+=2)
  40.          answer += vector1[k] * vector2[k]+vector1[k+1] * vector2[k+1];
  41.         return answer;
  42. }
  43.  
  44. uint64 test3() {// not a scalar product (fewer multiplications)
  45.         const int N = 1024*32;
  46.         uint64 vector1[N];
  47.         uint64 vector2[N];
  48.         uint64 answer;
  49.         for(int k =0; k+1<N;k+=2)
  50.          answer += ( vector1[k] + vector2[k] ) * ( vector1[k+1] + vector2[k+1] );
  51.         return answer;
  52. }
  53.  
  54. uint64 test4() {// standard scalar product with squares
  55.         const int N =1024*32;
  56.         uint64 vector1[N];
  57.         uint64 vector2[N];
  58.         uint64 answer;
  59.         for(int k =0; k<N;++k) {
  60.          answer += (vector1[k] + vector2[k])* (vector1[k] + vector2[k]);
  61.         }
  62.         return answer;
  63. }
  64.  
  65. uint64 test5() {//  scalar product 4x4
  66.         const int N = 1024*32;
  67.         uint64 vector1[N];
  68.         uint64 vector2[N];
  69.         uint64 answer;
  70.         for(int k =0; k+1<N;k+=4)
  71.          answer += vector1[k] * vector2[k]+vector1[k+1] * vector2[k+1]+vector1[k+2] * vector2[k+2]+vector1[k+3] * vector2[k+3];
  72.         return answer;
  73. }
  74.  
  75. uint64 test6() {//  just additions
  76.         const int N = 1024*32;
  77.         uint64 vector1[N];
  78.         uint64 vector2[N];
  79.         uint64 answer;
  80.         for(int k =0; k+1<N;k+=4)
  81.          answer += vector1[k] + vector2[k];
  82.         return answer;
  83. }
  84.  
  85. int main(int params, char ** args) {
  86.         ZTimer t;
  87.         uint64 fakesum;
  88.         double timeelapsed;
  89.         t.reset();
  90.         for(int loop = 1; loop<10000;++loop) fakesum += test1();
  91.         timeelapsed = t.split()/(1000.0);
  92.         cout<<timeelapsed<<endl;
  93.         t.reset();
  94.         for(int loop = 1; loop<10000;++loop) fakesum += test2();
  95.         timeelapsed = t.split()/(1000.0);
  96.         cout<<timeelapsed<<endl;
  97.         t.reset();
  98.         for(int loop = 1; loop<10000;++loop) fakesum += test3();
  99.         timeelapsed = t.split()/(1000.0);
  100.         cout<<timeelapsed<<endl;       
  101.         t.reset();
  102.         for(int loop = 1; loop<10000;++loop) fakesum += test4();
  103.         timeelapsed = t.split()/(1000.0);
  104.         cout<<timeelapsed<<endl;
  105.         t.reset();
  106.         for(int loop = 1; loop<10000;++loop) fakesum += test5();
  107.         timeelapsed = t.split()/(1000.0);
  108.         cout<<timeelapsed<<endl;
  109.         t.reset();
  110.         for(int loop = 1; loop<10000;++loop) fakesum += test6();
  111.         timeelapsed = t.split()/(1000.0);
  112.         cout<<timeelapsed<<endl;
  113.         cout<<endl;
  114.         cout<<fakesum<<endl;
  115. }