fork download
  1.  
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <cstdlib>
  6.  
  7. #include <sys/time.h>
  8. #include <unistd.h>
  9.  
  10. template <int x, int y>
  11. struct ArrayDim1 {
  12. int array[x*y];
  13.  
  14. int read(int nx, int ny ){
  15. return array[(ny*x)+nx];
  16. }
  17.  
  18. void write(int nx, int ny, int val ){
  19. array[(ny*x)+nx] = val;
  20. }
  21. };
  22.  
  23. template <int x, int y>
  24. struct ArrayDim2 {
  25. int array[y][x];
  26.  
  27. int read(int nx, int ny ){
  28. return array[ny][nx];
  29. }
  30.  
  31. void write(int nx, int ny, int val ){
  32. array[ny][nx] = val;
  33. }
  34. };
  35.  
  36. template<class Type>
  37. void seqRead(Type& a, int mx, int my){
  38. volatile int temp;
  39. for( int i = 0; i < my; i++ ){
  40. for( int j = 0; j < mx; j++ ){
  41. temp = a.read(i,j);
  42. }
  43. }
  44. }
  45.  
  46. template<class Type>
  47. void seqWrite(Type& a, int mx, int my){
  48. for( int i = 0; i < my; i++ ){
  49. for( int j = 0; j < mx; j++ ){
  50. a.write(i,j,i*j);
  51. }
  52. }
  53. }
  54.  
  55. struct Point {
  56. int x;
  57. int y;
  58. Point(){}
  59. Point(int nx,int ny): x(nx), y(ny) {}
  60. };
  61.  
  62. template<class Type>
  63. void randRead(Type& a, std::vector<Point>& addrs){
  64. std::vector<Point>::iterator it;
  65. volatile int temp;
  66.  
  67. Point p;
  68. for( it = addrs.begin(); it != addrs.end(); it++ ){
  69. p = *it;
  70. temp = a.read(p.x,p.y);
  71. }
  72. }
  73.  
  74. template<class Type>
  75. void randWrite(Type& a, std::vector<Point>& addrs){
  76. std::vector<Point>::iterator it;
  77. Point p;
  78. for( it = addrs.begin(); it != addrs.end(); it++ ){
  79. p = *it;
  80. a.write(p.x,p.y,p.x+p.y);
  81. }
  82. }
  83.  
  84.  
  85. std::vector<Point> randGen(int mx, int my){
  86. std::vector<Point> seq;
  87. std::vector<Point> r;
  88.  
  89. for( int y = 0; y < my; y++ ){
  90. for( int x = 0; x < mx; x++ ){
  91. seq.push_back(Point(x,y));
  92. }
  93. }
  94. while( seq.size() > 0 ){
  95. int n = std::rand() % seq.size();
  96. r.push_back( seq[n] );
  97. seq.erase( seq.begin() + n );
  98. }
  99. return r;
  100. }
  101.  
  102.  
  103. template <int x, int y>
  104. struct ArrayTest {
  105. typedef ArrayDim1<x,y> dim1;
  106. typedef ArrayDim2<x,y> dim2;
  107. typedef std::vector<Point> Ps;
  108.  
  109. enum {
  110. value_x = x,
  111. value_y = y
  112. };
  113.  
  114. dim1 a1;
  115. dim2 a2;
  116. Ps rnd;
  117.  
  118. ArrayTest(){
  119. this->rnd = randGen(x,y);
  120. seqWrite(a1,x,y);
  121. seqWrite(a2,x,y);
  122. }
  123.  
  124. void testSeqR1(){ seqRead(a1,x,y); }
  125. void testSeqR2(){ seqRead(a2,x,y); }
  126. void testSeqW1(){ seqWrite(a1,x,y); }
  127. void testSeqW2(){ seqWrite(a2,x,y); }
  128. void testRndR1(){ randRead(a1,rnd); }
  129. void testRndR2(){ randRead(a2,rnd); }
  130. void testRndW1(){ randWrite(a1,rnd); }
  131. void testRndW2(){ randWrite(a2,rnd); }
  132. };
  133.  
  134.  
  135.  
  136. long getUSec(){
  137. timeval tm;
  138. gettimeofday(&tm, NULL);
  139.  
  140. return (tm.tv_sec * 1000*1000) + tm.tv_usec;
  141. }
  142.  
  143. #define do_test_loop(c,x,y) \
  144.   { \
  145.   long st = getUSec(); \
  146.   cout << (y); \
  147.   for( int i_loop_ct = 0; i_loop_ct < (x); i_loop_ct++ ){\
  148.   c; \
  149.   } \
  150.   cout << (getUSec() - st) << endl; \
  151.   }
  152.  
  153. int main(){
  154. using namespace std;
  155.  
  156. typedef ArrayTest<256,256> AT;
  157.  
  158. cout << "begin initialize test object with Array " <<
  159. AT::value_x << "*" << AT::value_y
  160. << " (" <<
  161. ((AT::value_x * AT::value_y * sizeof(int)) / 1024)
  162. << " KiB)"<< endl;
  163.  
  164.  
  165. cout.flush();
  166.  
  167. AT* testMem = new AT();
  168.  
  169. AT& test = *testMem;
  170.  
  171.  
  172. cout << "... done" << endl;
  173.  
  174. cout << "[time unit: micro-sec]" << endl;
  175. do_test_loop( test.testSeqR1(), 1000,
  176. "sequential read x 1000: array[x*y] ... " );
  177.  
  178. do_test_loop( test.testSeqW1(), 1000,
  179. "sequential write x 1000: array[x*y] ... " );
  180.  
  181. do_test_loop( test.testRndR1(), 1000,
  182. "random read x 1000: array[x*y] ... " );
  183.  
  184. do_test_loop( test.testRndW1(), 1000,
  185. "random write x 1000: array[x*y] ... " );
  186.  
  187.  
  188.  
  189. do_test_loop( test.testSeqR2(), 1000,
  190. "sequential read x 1000: array[y][x] ... " );
  191.  
  192. do_test_loop( test.testSeqW2(), 1000,
  193. "sequential write x 1000: array[y][x] ... " );
  194.  
  195. do_test_loop( test.testRndR2(), 1000,
  196. "random read x 1000: array[y][x] ... " );
  197.  
  198. do_test_loop( test.testRndW2(), 1000,
  199. "random write x 1000: array[y][x] ... " );
  200.  
  201. return 0;
  202. }
  203.  
Success #stdin #stdout 4.25s 4464KB
stdin
Standard input is empty
stdout
begin initialize test object with Array 256*256 (256 KiB)
... done
[time unit: micro-sec]
sequential read  x 1000: array[x*y]  ... 246130
sequential write x 1000: array[x*y]  ... 380910
random     read  x 1000: array[x*y]  ... 292603
random     write x 1000: array[x*y]  ... 310411
sequential read  x 1000: array[y][x] ... 246199
sequential write x 1000: array[y][x] ... 259267
random     read  x 1000: array[y][x] ... 290635
random     write x 1000: array[y][x] ... 321935