nondet_random_speed.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* boost nondet_random_speed.cpp performance test
  2. *
  3. * Copyright Jens Maurer 2000
  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. * $Id$
  9. *
  10. */
  11. #include <iostream>
  12. #include <string>
  13. #include <boost/timer.hpp>
  14. #include <boost/random/random_device.hpp>
  15. // set to your CPU frequency
  16. static const double cpu_frequency = 1.87 * 1e9;
  17. static void show_elapsed(double end, int iter, const std::string & name)
  18. {
  19. double usec = end/iter*1e6;
  20. double cycles = usec * cpu_frequency/1e6;
  21. std::cout << name << ": "
  22. << usec*1e3 << " nsec/loop = "
  23. << cycles << " CPU cycles"
  24. << std::endl;
  25. }
  26. template<class Result, class RNG>
  27. static void timing(RNG & rng, int iter, const std::string& name)
  28. {
  29. volatile Result tmp; // make sure we're not optimizing too much
  30. boost::timer t;
  31. for(int i = 0; i < iter; i++)
  32. tmp = rng();
  33. show_elapsed(t.elapsed(), iter, name);
  34. }
  35. template<class RNG>
  36. void run(int iter, const std::string & name)
  37. {
  38. RNG rng;
  39. timing<long>(rng, iter, name);
  40. }
  41. int main(int argc, char*argv[])
  42. {
  43. if(argc != 2) {
  44. std::cerr << "usage: " << argv[0] << " iterations" << std::endl;
  45. return 1;
  46. }
  47. int iter = std::atoi(argv[1]);
  48. boost::random::random_device dev;
  49. timing<unsigned int>(dev, iter, "random_device");
  50. return 0;
  51. }