Advertisement
Guest User

Untitled

a guest
Dec 17th, 2011
2,860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. //#define TBB_TIMING
  7.  
  8. #ifdef TBB_TIMING  
  9. #include <tbb/tick_count.h>
  10. using tbb::tick_count;
  11. #else
  12. #include <time.h>
  13. #endif
  14.  
  15.  
  16.  
  17.  
  18. using namespace std;
  19.  
  20.  
  21.  
  22. //#define preallocate_memory new_cont
  23.  
  24. enum {new_cont,new_sep};
  25.  
  26.  
  27.  
  28. double *a1,*b1,*c1,*d1;
  29.  
  30.  
  31. void allo(int cont,int n){
  32.  
  33.     switch(cont){
  34.     case new_cont:
  35.         a1 = new double[n*4];
  36.         b1 = a1 + n;
  37.         c1 = b1 + n;
  38.         d1 = c1 + n;
  39.         break;
  40.     case new_sep:
  41.         a1 = new double[n];
  42.         b1 = new double[n];
  43.         c1 = new double[n];
  44.         d1 = new double[n];
  45.         break;
  46.     }
  47.     for(int i=0;i<n;i++){
  48.         a1[i]=1.0;d1[i]=1.0;c1[i]=1.0;b1[i]=1.0;
  49.     }
  50. }
  51.  
  52. void ff(int cont){
  53.  
  54.     switch(cont){
  55.     case new_sep:
  56.         delete[] b1;
  57.         delete[] c1;
  58.         delete[] d1;
  59.     case new_cont:
  60.         delete[] a1;
  61.     }
  62.    
  63. }
  64.  
  65.  
  66. double plain(int n,int m,int cont,int loops)
  67. {
  68. #ifndef preallocate_memory
  69.     allo(cont,n);
  70. #endif
  71.  
  72. #ifdef TBB_TIMING  
  73.     tick_count t0 = tick_count::now();
  74. #else
  75.     clock_t start = clock();
  76. #endif
  77.    
  78.    
  79.  
  80.     if(loops==1)
  81.         for(int i=0;i<m;i++)
  82.             for(int j=0;j<n;j++){
  83.                 a1[j] += b1[j];
  84.                 c1[j] += d1[j];
  85.         }
  86.     else
  87.         for(int i=0;i<m;i++){
  88.             for(int j=0;j<n;j++){
  89.                 a1[j] += b1[j];
  90.             }
  91.             for(int j=0;j<n;j++){
  92.                 c1[j] += d1[j];
  93.             }
  94.         }
  95.        
  96.  
  97.     double ret;
  98.  
  99. #ifdef TBB_TIMING  
  100.     tick_count t1 = tick_count::now();
  101.     ret=2.0*double(n)*double(m)/(t1-t0).seconds();
  102. #else
  103.     clock_t end = clock();
  104.     ret=2.0*double(n)*double(m)/(double)(end - start) *double(CLOCKS_PER_SEC);
  105. #endif
  106.    
  107. #ifndef preallocate_memory
  108.     ff(cont);
  109. #endif
  110.  
  111.     return ret;
  112. }
  113.  
  114.  
  115. void main(){
  116.    
  117.     freopen( "c:\\test.csv" ,"w",stdout);
  118.  
  119.     char* s=" ";
  120.  
  121.     string na[2] ={"new_cont","new_sep"};
  122.  
  123.     cout<<"n";
  124.  
  125.     for(int j=0;j<2;j++)
  126.         for(int i=1;i<=2;i++)
  127. #ifdef preallocate_memory
  128.             cout<<s<<i<<"_loops_"<<na[preallocate_memory];
  129. #else
  130.             cout<<s<<i<<"_loops_"<<na[j];
  131. #endif
  132.            
  133.     cout<<endl;
  134.  
  135.     long long nmax = 1000000;
  136.  
  137. #ifdef preallocate_memory
  138.     allo(preallocate_memory,nmax);
  139. #endif
  140.    
  141.     for(long long n=1;n<nmax;n=max(n+1,long long(n*1.2))){
  142.         const long long m=10000000/n;
  143.         cout<<n;
  144.         for(int j=0;j<2;j++)
  145.             for(int i=1;i<=2;i++)
  146.                 cout<<s<<plain(n,m,j,i);
  147.         cout<<endl;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement