perf_shared_mutex.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // (C) Copyright 2013 Andrey
  2. // (C) Copyright 2013 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // This performance test is based on the performance test provided by maxim.yegorushkin
  9. // at https://svn.boost.org/trac/boost/ticket/7422
  10. #define BOOST_THREAD_USES_CHRONO
  11. #include <iostream>
  12. #include <boost/thread/lock_types.hpp>
  13. #include <boost/thread/thread_only.hpp>
  14. #include <boost/chrono/chrono_io.hpp>
  15. #include <boost/thread/shared_mutex.hpp>
  16. using namespace boost;
  17. shared_mutex mtx;
  18. const int cycles = 10000;
  19. void shared()
  20. {
  21. int cycle(0);
  22. while (++cycle < cycles)
  23. {
  24. shared_lock<shared_mutex> lock(mtx);
  25. }
  26. }
  27. void unique()
  28. {
  29. int cycle(0);
  30. while (++cycle < cycles)
  31. {
  32. unique_lock<shared_mutex> lock(mtx);
  33. }
  34. }
  35. int main()
  36. {
  37. boost::chrono::high_resolution_clock::duration best_time(std::numeric_limits<boost::chrono::high_resolution_clock::duration::rep>::max BOOST_PREVENT_MACRO_SUBSTITUTION ());
  38. for (int i =100; i>0; --i) {
  39. boost::chrono::high_resolution_clock clock;
  40. boost::chrono::high_resolution_clock::time_point s1 = clock.now();
  41. thread t0(shared);
  42. thread t1(shared);
  43. thread t2(unique);
  44. //thread t11(shared);
  45. //thread t12(shared);
  46. //thread t13(shared);
  47. t0.join();
  48. t1.join();
  49. t2.join();
  50. //t11.join();
  51. // t12.join();
  52. // t13.join();
  53. boost::chrono::high_resolution_clock::time_point f1 = clock.now();
  54. //std::cout << " Time spent:" << (f1 - s1) << std::endl;
  55. best_time = std::min BOOST_PREVENT_MACRO_SUBSTITUTION (best_time, f1 - s1);
  56. }
  57. std::cout << "Best Time spent:" << best_time << std::endl;
  58. std::cout << "Time spent/cycle:" << best_time/cycles/3 << std::endl;
  59. return 1;
  60. }