Advertisement
Guest User

Untitled

a guest
Jul 19th, 2010
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  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 testp1() {// standard scalar product
  35.     const int N =1024*32;
  36.     uint64 vector1[N];
  37.     uint64 vector2[N];
  38.     uint64 answer;
  39.     uint64 * p1 = & vector1[0];
  40.     uint64 * p2 = & vector2[0];
  41.     for(int k =0; k<N;++k)
  42.      answer += *(p1++) * *(p2++);
  43.     return answer;
  44. }
  45.  
  46. uint64 test2() {//  scalar product 2x2
  47.     const int N = 1024*32;
  48.     uint64 vector1[N];
  49.     uint64 vector2[N];
  50.     uint64 answer;
  51.     for(int k =0; k+1<N;k+=2)
  52.      answer += vector1[k] * vector2[k]+vector1[k+1] * vector2[k+1];
  53.     return answer;
  54. }
  55.  
  56. uint64 testp2() {//  scalar product 2x2
  57.     const int N = 1024*32;
  58.     uint64 vector1[N];
  59.     uint64 vector2[N];
  60.     uint64 answer;
  61.     uint64 * p1 = & vector1[0];
  62.     uint64 * p2 = & vector2[0];
  63.     for(int k =0; k+1<N;k+=2)
  64.      answer += *(p1++) * *(p2++) +*(p1++) * *(p2++) ;
  65.     return answer;
  66. }
  67.  
  68. uint64 test3() {// not a scalar product (fewer multiplications)
  69.     const int N = 1024*32;
  70.     uint64 vector1[N];
  71.     uint64 vector2[N];
  72.     uint64 answer;
  73.     for(int k =0; k+1<N;k+=2)
  74.      answer += ( vector1[k] + vector2[k] ) * ( vector1[k+1] + vector2[k+1] );
  75.     return answer;
  76. }
  77.  
  78. uint64 testp3() {// not a scalar product (fewer multiplications)
  79.     const int N = 1024*32;
  80.     uint64 vector1[N];
  81.     uint64 vector2[N];
  82.     uint64 answer;
  83.     uint64 * p1 = & vector1[0];
  84.     uint64 * p2 = & vector2[0];
  85.     for(int k =0; k+1<N;k+=2)
  86.      answer += ( *(p1++) + *(p2++) ) * ( *(p1++) + *(p2++) );
  87.     return answer;
  88. }
  89.  
  90.  
  91. uint64 test4() {// standard scalar product with squares
  92.     const int N =1024*32;
  93.     uint64 vector1[N];
  94.     uint64 vector2[N];
  95.     uint64 answer;
  96.     for(int k =0; k<N;++k) {
  97.      answer += (vector1[k] + vector2[k])* (vector1[k] + vector2[k]);
  98.     }
  99.     return answer;
  100. }
  101.  
  102. uint64 test5() {//  scalar product 4x4
  103.     const int N = 1024*32;
  104.     uint64 vector1[N];
  105.     uint64 vector2[N];
  106.     uint64 answer;
  107.     for(int k =0; k+1<N;k+=4)
  108.      answer += vector1[k] * vector2[k]+vector1[k+1] * vector2[k+1]+vector1[k+2] * vector2[k+2]+vector1[k+3] * vector2[k+3];
  109.     return answer;
  110. }
  111.  
  112. uint64 test6() {//  just additions
  113.     const int N = 1024*32;
  114.     uint64 vector1[N];
  115.     uint64 vector2[N];
  116.     uint64 answer;
  117.     for(int k =0; k+1<N;k+=4)
  118.      answer += vector1[k] + vector2[k];
  119.     return answer;
  120. }
  121.  
  122. int main(int params, char ** args) {
  123.     ZTimer t;
  124.     uint64 fakesum;
  125.     double timeelapsed;
  126.     t.reset();
  127.     for(int loop = 1; loop<10000;++loop) fakesum += test1();
  128.     timeelapsed = t.split()/(1000.0);
  129.     cout<<"scalar = "<<timeelapsed<<endl;
  130.     t.reset();
  131.     for(int loop = 1; loop<10000;++loop) fakesum += testp1();
  132.     timeelapsed = t.split()/(1000.0);
  133.     cout<<"scalar with pointer = "<<timeelapsed<<endl;
  134.     t.reset();
  135.     for(int loop = 1; loop<10000;++loop) fakesum += test2();
  136.     timeelapsed = t.split()/(1000.0);
  137.     cout<<"scalar 2x2  = "<<timeelapsed<<endl;
  138.     t.reset();
  139.     for(int loop = 1; loop<10000;++loop) fakesum += testp2();
  140.     timeelapsed = t.split()/(1000.0);
  141.     cout<<"scalar 2x2 with pointer = "<<timeelapsed<<endl;
  142.     t.reset();
  143.     for(int loop = 1; loop<10000;++loop) fakesum += test3();
  144.     timeelapsed = t.split()/(1000.0);
  145.     cout<<timeelapsed<<endl;   
  146.     t.reset();
  147.     for(int loop = 1; loop<10000;++loop) fakesum += testp3();
  148.     timeelapsed = t.split()/(1000.0);
  149.     cout<<timeelapsed<<endl;   
  150.     /*t.reset();
  151.     for(int loop = 1; loop<10000;++loop) fakesum += test4();
  152.     timeelapsed = t.split()/(1000.0);
  153.     cout<<timeelapsed<<endl;
  154.     t.reset();
  155.     for(int loop = 1; loop<10000;++loop) fakesum += test5();
  156.     timeelapsed = t.split()/(1000.0);
  157.     cout<<timeelapsed<<endl;
  158.     t.reset();
  159.     for(int loop = 1; loop<10000;++loop) fakesum += test6();
  160.     timeelapsed = t.split()/(1000.0);
  161.     cout<<timeelapsed<<endl;*/
  162.     cout<<endl;
  163.     cout<<fakesum<<endl;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement