1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <boost/timer.hpp>
  5. #include <boost/numeric/ublas/matrix.hpp>
  6.  
  7. int main()
  8. {
  9. namespace ublas = boost::numeric::ublas;
  10. const int M = 10000, N = 20000;
  11.  
  12. // boost::ublas使用版
  13. {
  14. ublas::matrix<int> a( M, N );
  15.  
  16. boost::timer t;
  17. std::fill( a.data().begin(), a.data().end(), int( 0 ) );
  18. std::cout << t.elapsed() << std::endl;
  19. }
  20.  
  21. // std::vector使用版
  22. {
  23. using std::vector;
  24. vector<vector<int> > a( M, N );
  25.  
  26. boost::timer t;
  27. for( vector<vector<int> >::iterator i=a.begin(); i != a.end(); ++i ) {
  28. fill( i->begin(), i->end(), 0 );
  29. }
  30. std::cout << t.elapsed() << std::endl;
  31. }
  32.  
  33. // 配列使用版
  34. {
  35. int a[M][N];
  36.  
  37. boost::timer t;
  38. for( int i=0; i < M; ++i ) {
  39. for( int j=0; j < N; ++j ) a[i][j] = 0;
  40. }
  41. std::cout << t.elapsed() << std::endl;
  42. }
  43.  
  44. // 配列 (動的確保) 使用版
  45. {
  46. int **a = new int *[M]; // 行作成
  47. for( int i=0; i < M; ++i ) // 列作成
  48. a[i] = new int[N];
  49.  
  50. boost::timer t;
  51. for( int i=0; i < M; ++i ) {
  52. for( int j=0; j < N; ++j ) a[i][j] = 0;
  53. }
  54. std::cout << t.elapsed() << std::endl;
  55.  
  56. for( int i=0; i < M; ++i )
  57. delete [] a[i]; // 列解放
  58. delete [] a; // 行解放
  59. }
  60. }
  61.  
  62. 実行結果 :
  63. 0.781
  64. 0.484
  65. 0
  66. 1

タグ:

c++ tips
最終更新:2007年12月26日 20:11