path_times.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Boost Filesystem path_times.cpp ---------------------------------------------------//
  2. // Copyright Beman Dawes 2013
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/filesystem
  6. #include <boost/config/warning_disable.hpp>
  7. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  8. # define BOOST_FILESYSTEM_NO_DEPRECATED
  9. #endif
  10. #ifndef BOOST_SYSTEM_NO_DEPRECATED
  11. # define BOOST_SYSTEM_NO_DEPRECATED
  12. #endif
  13. #include <boost/timer/timer.hpp>
  14. #include <boost/filesystem/path.hpp>
  15. #include <boost/cstdint.hpp>
  16. #include <boost/config.hpp>
  17. # if defined( BOOST_NO_STD_WSTRING )
  18. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  19. # endif
  20. #include <boost/detail/lightweight_main.hpp>
  21. namespace fs = boost::filesystem;
  22. using namespace boost::timer;
  23. #include <fstream>
  24. #include <iostream>
  25. using std::cout;
  26. using std::endl;
  27. namespace
  28. {
  29. boost::int64_t max_cycles;
  30. template <class STD_STRING>
  31. nanosecond_type time_ctor(const STD_STRING& s)
  32. {
  33. boost::timer::auto_cpu_timer tmr;
  34. boost::int64_t count = 0;
  35. do
  36. {
  37. fs::path p(s);
  38. ++count;
  39. } while (count < max_cycles);
  40. boost::timer::cpu_times elapsed = tmr.elapsed();
  41. return elapsed.user + elapsed.system;
  42. }
  43. nanosecond_type time_loop()
  44. {
  45. boost::timer::auto_cpu_timer tmr;
  46. boost::int64_t count = 0;
  47. do
  48. {
  49. ++count;
  50. } while (count < max_cycles);
  51. boost::timer::cpu_times elapsed = tmr.elapsed();
  52. return elapsed.user + elapsed.system;
  53. }
  54. } // unnamed namespace
  55. //--------------------------------------------------------------------------------------//
  56. // main //
  57. //--------------------------------------------------------------------------------------//
  58. int cpp_main(int argc, char* argv[])
  59. {
  60. if (argc != 2)
  61. {
  62. cout << "Usage: path_times <cycles-in-millions>\n";
  63. return 1;
  64. }
  65. max_cycles = std::atoi(argv[1]) * 1000000LL;
  66. cout << "testing " << std::atoi(argv[1]) << " million cycles" << endl;
  67. cout << "time_loop" << endl;
  68. nanosecond_type x = time_loop();
  69. cout << "time_ctor with string" << endl;
  70. nanosecond_type s = time_ctor(std::string("/foo/bar/baz"));
  71. cout << "time_ctor with wstring" << endl;
  72. nanosecond_type w = time_ctor(std::wstring(L"/foo/bar/baz"));
  73. if (s > w)
  74. cout << "narrow/wide CPU-time ratio = " << long double(s)/w << endl;
  75. else
  76. cout << "wide/narrow CPU-time ratio = " << long double(w)/s << endl;
  77. cout << "returning from main()" << endl;
  78. return 0;
  79. }