timing_tests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // -----------------------------------------------------------
  2. //
  3. // Copyright (c) 2003-2004 Gennaro Prota
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // -----------------------------------------------------------
  10. // boost::dynamic_bitset timing tests
  11. //
  12. // NOTE:
  13. // ~~~~~
  14. // This is a preliminary, incomplete version.
  15. //
  16. // If you are interested in having more benchmarks please make a
  17. // request on the boost list, which could encourage me to continue
  18. // this work.
  19. // Also, if you use boost::dynamic_bitset on a platform where
  20. // CHAR_BIT >= 9 I suggest experimenting with the size of the count
  21. // table in detail/dynamic_bitset.hpp and report any interesting
  22. // discovery on the list as well.
  23. // You might also want to try both counting methods (by_bytes vs.
  24. // by_blocks) to see if the one that is selected automatically is
  25. // actually the fastest on your system.
  26. //
  27. //
  28. // -----------------------------------------------------------------------//
  29. #include "boost/config.hpp"
  30. #if defined (__STL_CONFIG_H) && !defined (__STL_USE_NEW_IOSTREAMS)
  31. // for pre 3.0 versions of libstdc++
  32. # define BOOST_OLD_IOSTREAMS
  33. #endif
  34. // ------------------------------------------------- //
  35. #include <typeinfo>
  36. #include <iostream>
  37. #if !defined(BOOST_OLD_IOSTREAMS)
  38. # include <ostream>
  39. #endif
  40. #include "boost/cstdlib.hpp"
  41. #include "boost/version.hpp"
  42. #include "boost/timer/timer.hpp"
  43. #include "boost/dynamic_bitset.hpp"
  44. namespace {
  45. // the m_ prefixes, below, are mainly to avoid problems with g++:
  46. // see http://gcc.gnu.org/ml/gcc-bugs/1999-03n/msg00884.html
  47. //
  48. class boost_version {
  49. int m_major;
  50. int m_minor;
  51. int m_subminor;
  52. public:
  53. boost_version(unsigned long v = BOOST_VERSION):
  54. m_major(v / 100000), m_minor(v / 100 % 1000), m_subminor(v % 100) {}
  55. friend std::ostream & operator<<(std::ostream &, const boost_version &);
  56. };
  57. // give up using basic_ostream, to avoid headaches with old libraries
  58. std::ostream& operator<<(std::ostream& os, const boost_version & v) {
  59. return os << v.m_major << '.' << v.m_minor << '.' << v.m_subminor;
  60. }
  61. }
  62. void prologue()
  63. {
  64. std::cout << '\n';
  65. std::cout << "Compiler: " << BOOST_COMPILER << '\n';
  66. std::cout << "Std lib : " << BOOST_STDLIB << '\n';
  67. std::cout << "Boost v.: " << boost_version() << '\n';
  68. std::cout << '\n';
  69. }
  70. template <typename T>
  71. void timing_test(T* = 0) // dummy parameter to workaround VC6
  72. {
  73. #ifndef BOOST_NO_STRESS_TEST
  74. const unsigned long num = 30 * 100000;
  75. #else
  76. const unsigned long num = 30 * 1000;
  77. #endif
  78. // This variable is printed at the end of the test,
  79. // to prevent the optimizer from removing the call to
  80. // count() in the loop below.
  81. typename boost::dynamic_bitset<T>::size_type dummy = 0;
  82. std::cout << "\nTimings for dynamic_bitset<" << typeid(T).name()
  83. << "> [" << num << " iterations]\n";
  84. std::cout << "--------------------------------------------------\n";
  85. {
  86. boost::timer::auto_cpu_timer time;
  87. const typename boost::dynamic_bitset<T>::size_type sz = 5000;
  88. for (unsigned long i = 0; i < num; ++i) {
  89. boost::dynamic_bitset<T> bs(sz, i);
  90. dummy += bs.count();
  91. }
  92. }
  93. std::cout << "(total count: " << dummy << ")\n\n";
  94. }
  95. int main()
  96. {
  97. prologue();
  98. timing_test<unsigned char>();
  99. timing_test<unsigned short>();
  100. timing_test<unsigned int>();
  101. timing_test<unsigned long>();
  102. # ifdef BOOST_HAS_LONG_LONG
  103. timing_test< ::boost::ulong_long_type>();
  104. # endif
  105. return boost::exit_success;
  106. }