Shuto Naruse
/
Eurobot2012_Secondary
Eurobot2012_Secondary
Fork of Eurobot_2012_Secondary by
tvmet/util/Timer.h@1:cc2a9eb0bd55, 2012-10-17 (annotated)
- Committer:
- narshu
- Date:
- Wed Oct 17 22:25:31 2012 +0000
- Revision:
- 1:cc2a9eb0bd55
Commit before publishing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 1:cc2a9eb0bd55 | 1 | /* |
narshu | 1:cc2a9eb0bd55 | 2 | * Tiny Vector Matrix Library |
narshu | 1:cc2a9eb0bd55 | 3 | * Dense Vector Matrix Libary of Tiny size using Expression Templates |
narshu | 1:cc2a9eb0bd55 | 4 | * |
narshu | 1:cc2a9eb0bd55 | 5 | * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> |
narshu | 1:cc2a9eb0bd55 | 6 | * |
narshu | 1:cc2a9eb0bd55 | 7 | * This library is free software; you can redistribute it and/or |
narshu | 1:cc2a9eb0bd55 | 8 | * modify it under the terms of the GNU Lesser General Public |
narshu | 1:cc2a9eb0bd55 | 9 | * License as published by the Free Software Foundation; either |
narshu | 1:cc2a9eb0bd55 | 10 | * version 2.1 of the License, or (at your option) any later version. |
narshu | 1:cc2a9eb0bd55 | 11 | * |
narshu | 1:cc2a9eb0bd55 | 12 | * This library is distributed in the hope that it will be useful, |
narshu | 1:cc2a9eb0bd55 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
narshu | 1:cc2a9eb0bd55 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
narshu | 1:cc2a9eb0bd55 | 15 | * Lesser General Public License for more details. |
narshu | 1:cc2a9eb0bd55 | 16 | * |
narshu | 1:cc2a9eb0bd55 | 17 | * You should have received a copy of the GNU Lesser General Public |
narshu | 1:cc2a9eb0bd55 | 18 | * License along with this library; if not, write to the Free Software |
narshu | 1:cc2a9eb0bd55 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
narshu | 1:cc2a9eb0bd55 | 20 | * |
narshu | 1:cc2a9eb0bd55 | 21 | * $Id: Timer.h,v 1.9 2007-06-23 15:58:59 opetzold Exp $ |
narshu | 1:cc2a9eb0bd55 | 22 | */ |
narshu | 1:cc2a9eb0bd55 | 23 | |
narshu | 1:cc2a9eb0bd55 | 24 | #ifndef TVMET_UTIL_TIMER_H |
narshu | 1:cc2a9eb0bd55 | 25 | #define TVMET_UTIL_TIMER_H |
narshu | 1:cc2a9eb0bd55 | 26 | |
narshu | 1:cc2a9eb0bd55 | 27 | #if defined(TVMET_HAVE_SYS_TIME_H) && defined(TVMET_HAVE_UNISTD_H) |
narshu | 1:cc2a9eb0bd55 | 28 | # include <sys/time.h> |
narshu | 1:cc2a9eb0bd55 | 29 | # include <sys/resource.h> |
narshu | 1:cc2a9eb0bd55 | 30 | # include <unistd.h> |
narshu | 1:cc2a9eb0bd55 | 31 | #else |
narshu | 1:cc2a9eb0bd55 | 32 | # include <ctime> |
narshu | 1:cc2a9eb0bd55 | 33 | #endif |
narshu | 1:cc2a9eb0bd55 | 34 | |
narshu | 1:cc2a9eb0bd55 | 35 | namespace tvmet { |
narshu | 1:cc2a9eb0bd55 | 36 | |
narshu | 1:cc2a9eb0bd55 | 37 | namespace util { |
narshu | 1:cc2a9eb0bd55 | 38 | |
narshu | 1:cc2a9eb0bd55 | 39 | /** |
narshu | 1:cc2a9eb0bd55 | 40 | \class Timer Timer.h "tvmet/util/Timer.h" |
narshu | 1:cc2a9eb0bd55 | 41 | \brief A quick& dirty portable timer, measures elapsed time. |
narshu | 1:cc2a9eb0bd55 | 42 | |
narshu | 1:cc2a9eb0bd55 | 43 | It is recommended that implementations measure wall clock rather than CPU |
narshu | 1:cc2a9eb0bd55 | 44 | time since the intended use is performance measurement on systems where |
narshu | 1:cc2a9eb0bd55 | 45 | total elapsed time is more important than just process or CPU time. |
narshu | 1:cc2a9eb0bd55 | 46 | |
narshu | 1:cc2a9eb0bd55 | 47 | The accuracy of timings depends on the accuracy of timing information |
narshu | 1:cc2a9eb0bd55 | 48 | provided by the underlying platform, and this varies from platform to |
narshu | 1:cc2a9eb0bd55 | 49 | platform. |
narshu | 1:cc2a9eb0bd55 | 50 | */ |
narshu | 1:cc2a9eb0bd55 | 51 | |
narshu | 1:cc2a9eb0bd55 | 52 | class Timer |
narshu | 1:cc2a9eb0bd55 | 53 | { |
narshu | 1:cc2a9eb0bd55 | 54 | Timer(const Timer&); |
narshu | 1:cc2a9eb0bd55 | 55 | Timer& operator=(const Timer&); |
narshu | 1:cc2a9eb0bd55 | 56 | |
narshu | 1:cc2a9eb0bd55 | 57 | public: // types |
narshu | 1:cc2a9eb0bd55 | 58 | typedef double time_t; |
narshu | 1:cc2a9eb0bd55 | 59 | |
narshu | 1:cc2a9eb0bd55 | 60 | public: |
narshu | 1:cc2a9eb0bd55 | 61 | /** starts the timer immediatly. */ |
narshu | 1:cc2a9eb0bd55 | 62 | Timer() { m_start_time = getTime(); } |
narshu | 1:cc2a9eb0bd55 | 63 | |
narshu | 1:cc2a9eb0bd55 | 64 | /** restarts the timer */ |
narshu | 1:cc2a9eb0bd55 | 65 | void restart() { m_start_time = getTime(); } |
narshu | 1:cc2a9eb0bd55 | 66 | |
narshu | 1:cc2a9eb0bd55 | 67 | /** return elapsed time in seconds */ |
narshu | 1:cc2a9eb0bd55 | 68 | time_t elapsed() const { return (getTime() - m_start_time); } |
narshu | 1:cc2a9eb0bd55 | 69 | |
narshu | 1:cc2a9eb0bd55 | 70 | private: |
narshu | 1:cc2a9eb0bd55 | 71 | time_t getTime() const { |
narshu | 1:cc2a9eb0bd55 | 72 | #if defined(TVMET_HAVE_SYS_TIME_H) && defined(TVMET_HAVE_UNISTD_H) |
narshu | 1:cc2a9eb0bd55 | 73 | getrusage(RUSAGE_SELF, &m_rusage); |
narshu | 1:cc2a9eb0bd55 | 74 | time_t sec = m_rusage.ru_utime.tv_sec; // user, no system time |
narshu | 1:cc2a9eb0bd55 | 75 | time_t usec = m_rusage.ru_utime.tv_usec; // user, no system time |
narshu | 1:cc2a9eb0bd55 | 76 | return sec + usec/1e6; |
narshu | 1:cc2a9eb0bd55 | 77 | #else |
narshu | 1:cc2a9eb0bd55 | 78 | return static_cast<time_t>(std::clock()) / static_cast<time_t>(CLOCKS_PER_SEC); |
narshu | 1:cc2a9eb0bd55 | 79 | #endif |
narshu | 1:cc2a9eb0bd55 | 80 | } |
narshu | 1:cc2a9eb0bd55 | 81 | |
narshu | 1:cc2a9eb0bd55 | 82 | private: |
narshu | 1:cc2a9eb0bd55 | 83 | #if defined(TVMET_HAVE_SYS_TIME_H) && defined(TVMET_HAVE_UNISTD_H) |
narshu | 1:cc2a9eb0bd55 | 84 | mutable struct rusage m_rusage; |
narshu | 1:cc2a9eb0bd55 | 85 | #endif |
narshu | 1:cc2a9eb0bd55 | 86 | time_t m_start_time; |
narshu | 1:cc2a9eb0bd55 | 87 | }; |
narshu | 1:cc2a9eb0bd55 | 88 | |
narshu | 1:cc2a9eb0bd55 | 89 | } // namespace util |
narshu | 1:cc2a9eb0bd55 | 90 | |
narshu | 1:cc2a9eb0bd55 | 91 | } // namespace tvmet |
narshu | 1:cc2a9eb0bd55 | 92 | |
narshu | 1:cc2a9eb0bd55 | 93 | #endif // TVMET_UTIL_TIMER_H |
narshu | 1:cc2a9eb0bd55 | 94 | |
narshu | 1:cc2a9eb0bd55 | 95 | // Local Variables: |
narshu | 1:cc2a9eb0bd55 | 96 | // mode:C++ |
narshu | 1:cc2a9eb0bd55 | 97 | // tab-width:8 |
narshu | 1:cc2a9eb0bd55 | 98 | // End: |