PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/exp/js/Timer.cpp

https://github.com/sofian/drone
C++ | 121 lines | 63 code | 13 blank | 45 comment | 4 complexity | bfcd169fd39eb0017ecb13a5643684d8 MD5 | raw file
  1. /* Timer.cpp
  2. * Copyright (C) 2004 Mathieu Guindon, Julien Keable
  3. * This file is part of Drone.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. // Copyright (C) 2004 Ronan Collobert (collober@idiap.ch)
  20. //
  21. // This file is part of Torch 3.
  22. //
  23. // All rights reserved.
  24. //
  25. // Redistribution and use in source and binary forms, with or without
  26. // modification, are permitted provided that the following conditions
  27. // are met:
  28. // 1. Redistributions of source code must retain the above copyright
  29. // notice, this list of conditions and the following disclaimer.
  30. // 2. Redistributions in binary form must reproduce the above copyright
  31. // notice, this list of conditions and the following disclaimer in the
  32. // documentation and/or other materials provided with the distribution.
  33. // 3. The name of the author may not be used to endorse or promote products
  34. // derived from this software without specific prior written permission.
  35. //
  36. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  37. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  39. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  45. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. #include "Timer.h"
  47. #ifndef _MSC_VER
  48. #include <sys/times.h>
  49. #include <unistd.h>
  50. #endif
  51. #ifdef _MSC_VER
  52. time_t Timer::_base_time = 0;
  53. #endif
  54. float Timer::getRunTime()
  55. {
  56. #ifdef _MSC_VER
  57. time_t truc_foireux;
  58. time(&truc_foireux);
  59. return(difftime(truc_foireux, base_time));
  60. #else
  61. struct tms current;
  62. times(&current);
  63. float norm = (float)sysconf(_SC_CLK_TCK);
  64. return(((float)current.tms_utime)/norm);
  65. #endif
  66. }
  67. Timer::Timer()
  68. {
  69. #ifdef _MSC_VER
  70. while (!base_time)
  71. time(&base_time);
  72. #endif
  73. _totalTime = 0;
  74. _isRunning = true;
  75. _startTime = getRunTime();
  76. }
  77. void Timer::reset()
  78. {
  79. _totalTime = 0;
  80. _startTime = getRunTime();
  81. }
  82. void Timer::stop()
  83. {
  84. if (!_isRunning)
  85. return;
  86. float _currentTime = getRunTime() - _startTime;
  87. _totalTime += _currentTime;
  88. _isRunning = false;
  89. }
  90. void Timer::resume()
  91. {
  92. if (_isRunning)
  93. return;
  94. _startTime = getRunTime();
  95. _isRunning = true;
  96. }
  97. float Timer::getTime()
  98. {
  99. if (_isRunning)
  100. {
  101. float _currentTime = getRunTime() - _startTime;
  102. return(_totalTime+_currentTime);
  103. } else
  104. return _totalTime;
  105. }
  106. Timer::~Timer()
  107. {
  108. }