You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

  1. #define NOGMM
  2. #define NOMTL
  3. #include <map>
  4. #include <ext/hash_map>
  5. #include <google/dense_hash_map>
  6. #include <google/sparse_hash_map>
  7. #ifndef SIZE
  8. #define SIZE 10000
  9. #endif
  10. #ifndef DENSITY
  11. #define DENSITY 0.01
  12. #endif
  13. #ifndef REPEAT
  14. #define REPEAT 1
  15. #endif
  16. #include "BenchSparseUtil.h"
  17. #ifndef MINDENSITY
  18. #define MINDENSITY 0.0004
  19. #endif
  20. #ifndef NBTRIES
  21. #define NBTRIES 10
  22. #endif
  23. #define BENCH(X) \
  24. timer.reset(); \
  25. for (int _j=0; _j<NBTRIES; ++_j) { \
  26. timer.start(); \
  27. for (int _k=0; _k<REPEAT; ++_k) { \
  28. X \
  29. } timer.stop(); }
  30. static double rtime;
  31. static double nentries;
  32. template<typename SetterType>
  33. void dostuff(const char* name, EigenSparseMatrix& sm1)
  34. {
  35. int rows = sm1.rows();
  36. int cols = sm1.cols();
  37. sm1.setZero();
  38. BenchTimer t;
  39. SetterType* set1 = new SetterType(sm1);
  40. t.reset(); t.start();
  41. for (int k=0; k<nentries; ++k)
  42. (*set1)(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
  43. t.stop();
  44. std::cout << "std::map => \t" << t.value()-rtime
  45. << " nnz=" << set1->nonZeros() << std::flush;
  46. // getchar();
  47. t.reset(); t.start(); delete set1; t.stop();
  48. std::cout << " back: \t" << t.value() << "\n";
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. int rows = SIZE;
  53. int cols = SIZE;
  54. float density = DENSITY;
  55. EigenSparseMatrix sm1(rows,cols), sm2(rows,cols);
  56. nentries = rows*cols*density;
  57. std::cout << "n = " << nentries << "\n";
  58. int dummy;
  59. BenchTimer t;
  60. t.reset(); t.start();
  61. for (int k=0; k<nentries; ++k)
  62. dummy = internal::random<int>(0,rows-1) + internal::random<int>(0,cols-1);
  63. t.stop();
  64. rtime = t.value();
  65. std::cout << "rtime = " << rtime << " (" << dummy << ")\n\n";
  66. const int Bits = 6;
  67. for (;;)
  68. {
  69. dostuff<RandomSetter<EigenSparseMatrix,StdMapTraits,Bits> >("std::map ", sm1);
  70. dostuff<RandomSetter<EigenSparseMatrix,GnuHashMapTraits,Bits> >("gnu::hash_map", sm1);
  71. dostuff<RandomSetter<EigenSparseMatrix,GoogleDenseHashMapTraits,Bits> >("google::dense", sm1);
  72. dostuff<RandomSetter<EigenSparseMatrix,GoogleSparseHashMapTraits,Bits> >("google::sparse", sm1);
  73. // {
  74. // RandomSetter<EigenSparseMatrix,GnuHashMapTraits,Bits> set1(sm1);
  75. // t.reset(); t.start();
  76. // for (int k=0; k<n; ++k)
  77. // set1(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
  78. // t.stop();
  79. // std::cout << "gnu::hash_map => \t" << t.value()-rtime
  80. // << " nnz=" << set1.nonZeros() << "\n";getchar();
  81. // }
  82. // {
  83. // RandomSetter<EigenSparseMatrix,GoogleDenseHashMapTraits,Bits> set1(sm1);
  84. // t.reset(); t.start();
  85. // for (int k=0; k<n; ++k)
  86. // set1(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
  87. // t.stop();
  88. // std::cout << "google::dense => \t" << t.value()-rtime
  89. // << " nnz=" << set1.nonZeros() << "\n";getchar();
  90. // }
  91. // {
  92. // RandomSetter<EigenSparseMatrix,GoogleSparseHashMapTraits,Bits> set1(sm1);
  93. // t.reset(); t.start();
  94. // for (int k=0; k<n; ++k)
  95. // set1(internal::random<int>(0,rows-1),internal::random<int>(0,cols-1)) += 1;
  96. // t.stop();
  97. // std::cout << "google::sparse => \t" << t.value()-rtime
  98. // << " nnz=" << set1.nonZeros() << "\n";getchar();
  99. // }
  100. std::cout << "\n\n";
  101. }
  102. return 0;
  103. }