wall_clock_meat.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup wall_clock
  16. //! @{
  17. inline
  18. wall_clock::wall_clock()
  19. : valid(false)
  20. {
  21. arma_extra_debug_sigprint();
  22. }
  23. inline
  24. wall_clock::~wall_clock()
  25. {
  26. arma_extra_debug_sigprint();
  27. }
  28. inline
  29. void
  30. wall_clock::tic()
  31. {
  32. arma_extra_debug_sigprint();
  33. #if defined(ARMA_USE_CXX11)
  34. {
  35. chrono_time1 = std::chrono::steady_clock::now();
  36. valid = true;
  37. }
  38. #elif defined(ARMA_HAVE_GETTIMEOFDAY)
  39. {
  40. gettimeofday(&posix_time1, 0);
  41. valid = true;
  42. }
  43. #else
  44. {
  45. time1 = std::clock();
  46. valid = true;
  47. }
  48. #endif
  49. }
  50. inline
  51. double
  52. wall_clock::toc()
  53. {
  54. arma_extra_debug_sigprint();
  55. if(valid)
  56. {
  57. #if defined(ARMA_USE_CXX11)
  58. {
  59. const std::chrono::steady_clock::time_point chrono_time2 = std::chrono::steady_clock::now();
  60. typedef std::chrono::duration<double> duration_type;
  61. const duration_type chrono_span = std::chrono::duration_cast< duration_type >(chrono_time2 - chrono_time1);
  62. return chrono_span.count();
  63. }
  64. #elif defined(ARMA_HAVE_GETTIMEOFDAY)
  65. {
  66. gettimeofday(&posix_time2, 0);
  67. const double tmp_time1 = double(posix_time1.tv_sec) + double(posix_time1.tv_usec) * 1.0e-6;
  68. const double tmp_time2 = double(posix_time2.tv_sec) + double(posix_time2.tv_usec) * 1.0e-6;
  69. return tmp_time2 - tmp_time1;
  70. }
  71. #else
  72. {
  73. std::clock_t time2 = std::clock();
  74. std::clock_t diff = time2 - time1;
  75. return double(diff) / double(CLOCKS_PER_SEC);
  76. }
  77. #endif
  78. }
  79. else
  80. {
  81. return 0.0;
  82. }
  83. }
  84. //! @}