Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
timer.h@0:ee91220d7bea, 2010-11-30 (annotated)
- Committer:
- taoxh
- Date:
- Tue Nov 30 23:53:06 2010 +0000
- Revision:
- 0:ee91220d7bea
HW2 PROTOTHREAD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taoxh | 0:ee91220d7bea | 1 | #ifndef TIMER_H |
taoxh | 0:ee91220d7bea | 2 | #define TIMER_H |
taoxh | 0:ee91220d7bea | 3 | |
taoxh | 0:ee91220d7bea | 4 | #include <ctime> |
taoxh | 0:ee91220d7bea | 5 | #include <iostream> |
taoxh | 0:ee91220d7bea | 6 | #include <iomanip> |
taoxh | 0:ee91220d7bea | 7 | |
taoxh | 0:ee91220d7bea | 8 | class timer |
taoxh | 0:ee91220d7bea | 9 | { |
taoxh | 0:ee91220d7bea | 10 | friend std::ostream& operator<<(std::ostream& os, timer& t); |
taoxh | 0:ee91220d7bea | 11 | |
taoxh | 0:ee91220d7bea | 12 | private: |
taoxh | 0:ee91220d7bea | 13 | bool running; |
taoxh | 0:ee91220d7bea | 14 | clock_t start_clock; |
taoxh | 0:ee91220d7bea | 15 | time_t start_time; |
taoxh | 0:ee91220d7bea | 16 | double acc_time; |
taoxh | 0:ee91220d7bea | 17 | |
taoxh | 0:ee91220d7bea | 18 | double elapsed_time(); |
taoxh | 0:ee91220d7bea | 19 | |
taoxh | 0:ee91220d7bea | 20 | public: |
taoxh | 0:ee91220d7bea | 21 | // 'running' is initially false. A timer needs to be explicitly started |
taoxh | 0:ee91220d7bea | 22 | // using 'start' or 'restart' |
taoxh | 0:ee91220d7bea | 23 | timer() : running(false), start_clock(0), start_time(0), acc_time(0) { } |
taoxh | 0:ee91220d7bea | 24 | |
taoxh | 0:ee91220d7bea | 25 | void start(const char* msg = 0); |
taoxh | 0:ee91220d7bea | 26 | void restart(const char* msg = 0); |
taoxh | 0:ee91220d7bea | 27 | void stop(const char* msg = 0); |
taoxh | 0:ee91220d7bea | 28 | void check(const char* msg = 0); |
taoxh | 0:ee91220d7bea | 29 | |
taoxh | 0:ee91220d7bea | 30 | }; // class timer |
taoxh | 0:ee91220d7bea | 31 | |
taoxh | 0:ee91220d7bea | 32 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 33 | // Return the total time that the timer has been in the "running" |
taoxh | 0:ee91220d7bea | 34 | // state since it was first "started" or last "restarted". For |
taoxh | 0:ee91220d7bea | 35 | // "short" time periods (less than an hour), the actual cpu time |
taoxh | 0:ee91220d7bea | 36 | // used is reported instead of the elapsed time. |
taoxh | 0:ee91220d7bea | 37 | |
taoxh | 0:ee91220d7bea | 38 | inline double timer::elapsed_time() |
taoxh | 0:ee91220d7bea | 39 | { |
taoxh | 0:ee91220d7bea | 40 | time_t acc_sec = time(0) - start_time; |
taoxh | 0:ee91220d7bea | 41 | if (acc_sec < 3600) |
taoxh | 0:ee91220d7bea | 42 | return (clock() - start_clock) / (1.0 * CLOCKS_PER_SEC); |
taoxh | 0:ee91220d7bea | 43 | else |
taoxh | 0:ee91220d7bea | 44 | return (1.0 * acc_sec); |
taoxh | 0:ee91220d7bea | 45 | |
taoxh | 0:ee91220d7bea | 46 | } // timer::elapsed_time |
taoxh | 0:ee91220d7bea | 47 | |
taoxh | 0:ee91220d7bea | 48 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 49 | // Start a timer. If it is already running, let it continue running. |
taoxh | 0:ee91220d7bea | 50 | // Print an optional message. |
taoxh | 0:ee91220d7bea | 51 | |
taoxh | 0:ee91220d7bea | 52 | inline void timer::start(const char* msg) |
taoxh | 0:ee91220d7bea | 53 | { |
taoxh | 0:ee91220d7bea | 54 | // Print an optional message, something like "Starting timer t"; |
taoxh | 0:ee91220d7bea | 55 | if (msg) std::cout << msg << std::endl; |
taoxh | 0:ee91220d7bea | 56 | |
taoxh | 0:ee91220d7bea | 57 | // Return immediately if the timer is already running |
taoxh | 0:ee91220d7bea | 58 | if (running) return; |
taoxh | 0:ee91220d7bea | 59 | |
taoxh | 0:ee91220d7bea | 60 | // Set timer status to running and set the start time |
taoxh | 0:ee91220d7bea | 61 | running = true; |
taoxh | 0:ee91220d7bea | 62 | start_clock = clock(); |
taoxh | 0:ee91220d7bea | 63 | start_time = time(0); |
taoxh | 0:ee91220d7bea | 64 | |
taoxh | 0:ee91220d7bea | 65 | } // timer::start |
taoxh | 0:ee91220d7bea | 66 | |
taoxh | 0:ee91220d7bea | 67 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 68 | // Turn the timer off and start it again from 0. Print an optional message. |
taoxh | 0:ee91220d7bea | 69 | |
taoxh | 0:ee91220d7bea | 70 | inline void timer::restart(const char* msg) |
taoxh | 0:ee91220d7bea | 71 | { |
taoxh | 0:ee91220d7bea | 72 | // Print an optional message, something like "Restarting timer t"; |
taoxh | 0:ee91220d7bea | 73 | if (msg) std::cout << msg << std::endl; |
taoxh | 0:ee91220d7bea | 74 | |
taoxh | 0:ee91220d7bea | 75 | // Set timer status to running, reset accumulated time, and set start time |
taoxh | 0:ee91220d7bea | 76 | running = true; |
taoxh | 0:ee91220d7bea | 77 | acc_time = 0; |
taoxh | 0:ee91220d7bea | 78 | start_clock = clock(); |
taoxh | 0:ee91220d7bea | 79 | start_time = time(0); |
taoxh | 0:ee91220d7bea | 80 | |
taoxh | 0:ee91220d7bea | 81 | } // timer::restart |
taoxh | 0:ee91220d7bea | 82 | |
taoxh | 0:ee91220d7bea | 83 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 84 | // Stop the timer and print an optional message. |
taoxh | 0:ee91220d7bea | 85 | |
taoxh | 0:ee91220d7bea | 86 | inline void timer::stop(const char* msg) |
taoxh | 0:ee91220d7bea | 87 | { |
taoxh | 0:ee91220d7bea | 88 | // Print an optional message, something like "Stopping timer t"; |
taoxh | 0:ee91220d7bea | 89 | if (msg) std::cout << msg << std::endl; |
taoxh | 0:ee91220d7bea | 90 | |
taoxh | 0:ee91220d7bea | 91 | // Compute accumulated running time and set timer status to not running |
taoxh | 0:ee91220d7bea | 92 | if (running) acc_time += elapsed_time(); |
taoxh | 0:ee91220d7bea | 93 | running = false; |
taoxh | 0:ee91220d7bea | 94 | |
taoxh | 0:ee91220d7bea | 95 | } // timer::stop |
taoxh | 0:ee91220d7bea | 96 | |
taoxh | 0:ee91220d7bea | 97 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 98 | // Print out an optional message followed by the current timer timing. |
taoxh | 0:ee91220d7bea | 99 | |
taoxh | 0:ee91220d7bea | 100 | inline void timer::check(const char* msg) |
taoxh | 0:ee91220d7bea | 101 | { |
taoxh | 0:ee91220d7bea | 102 | // Print an optional message, something like "Checking timer t"; |
taoxh | 0:ee91220d7bea | 103 | if (msg) std::cout << msg << " : "; |
taoxh | 0:ee91220d7bea | 104 | |
taoxh | 0:ee91220d7bea | 105 | std::cout << "Elapsed time [" << std::setiosflags(std::ios::fixed) |
taoxh | 0:ee91220d7bea | 106 | << std::setprecision(2) |
taoxh | 0:ee91220d7bea | 107 | << acc_time + (running ? elapsed_time() : 0) << "] seconds\n"; |
taoxh | 0:ee91220d7bea | 108 | |
taoxh | 0:ee91220d7bea | 109 | } // timer::check |
taoxh | 0:ee91220d7bea | 110 | |
taoxh | 0:ee91220d7bea | 111 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 112 | // Allow timers to be printed to ostreams using the syntax 'os << t' |
taoxh | 0:ee91220d7bea | 113 | // for an ostream 'os' and a timer 't'. For example, "cout << t" will |
taoxh | 0:ee91220d7bea | 114 | // print out the total amount of time 't' has been "running". |
taoxh | 0:ee91220d7bea | 115 | |
taoxh | 0:ee91220d7bea | 116 | inline std::ostream& operator<<(std::ostream& os, timer& t) |
taoxh | 0:ee91220d7bea | 117 | { |
taoxh | 0:ee91220d7bea | 118 | os << std::setprecision(2) << std::setiosflags(std::ios::fixed) |
taoxh | 0:ee91220d7bea | 119 | << t.acc_time + (t.running ? t.elapsed_time() : 0); |
taoxh | 0:ee91220d7bea | 120 | return os; |
taoxh | 0:ee91220d7bea | 121 | } |
taoxh | 0:ee91220d7bea | 122 | |
taoxh | 0:ee91220d7bea | 123 | //=========================================================================== |
taoxh | 0:ee91220d7bea | 124 | |
taoxh | 0:ee91220d7bea | 125 | #endif // TIMER_H |
taoxh | 0:ee91220d7bea | 126 |