Lemma is an Electromagnetics API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.h 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author Trevor Irons
  8. @date 01/27/2010
  9. @version $Id: timer.h 87 2013-09-05 22:44:05Z tirons $
  10. **/
  11. #ifndef TIMER_H_INC
  12. #define TIMER_H_INC
  13. #include <iostream>
  14. #include <fstream>
  15. #include <ctime>
  16. #ifdef LEMMAUSEOMP
  17. #include "omp.h"
  18. #endif
  19. #include "lemma.h"
  20. namespace Lemma {
  21. /** \brief Simple timer class.
  22. * \details Can be used either with multithreaded applications, using
  23. * OpenMP defined functions, or using system wall time. System wall time
  24. * is not good for parallel programs as it reports cpu core execution time.
  25. */
  26. class jsw_timer {
  27. public:
  28. /**
  29. * Constructor that automatically starts the timer.
  30. */
  31. jsw_timer(): start ( std::clock() ), elapsed ( 0.0 ) {
  32. }
  33. /** begins the timer.
  34. */
  35. void begin() {
  36. #ifdef LEMMAUSEOMP
  37. start = omp_get_wtime();
  38. #else
  39. start = std::clock();
  40. #endif
  41. elapsed = 0.0;
  42. }
  43. /** Ends the timer.
  44. * @return the time that the stopwatch ended at.
  45. */
  46. Real end() {
  47. #ifdef LEMMAUSEOMP
  48. elapsed = static_cast<Real> (omp_get_wtime()) - start;
  49. return elapsed;
  50. #else
  51. elapsed = static_cast<Real> ( std::clock() ) - start;
  52. return elapsed /= CLOCKS_PER_SEC;
  53. #endif
  54. }
  55. /**
  56. * @return the elapsed time between start and end.
  57. */
  58. Real last() const { return elapsed; }
  59. private:
  60. std::clock_t start;
  61. Real elapsed;
  62. };
  63. } // ----- end of Lemma name -----
  64. #endif // ----- #ifndef TIMER_H_INC -----