diff --git a/src/support/timing.h b/src/support/timing.h index 4179d2e3064..4521d5b6812 100644 --- a/src/support/timing.h +++ b/src/support/timing.h @@ -14,38 +14,53 @@ * limitations under the License. */ -// -// Timing helper -// - #ifndef wasm_support_timing_h #define wasm_support_timing_h #include -#include -#include +#include namespace wasm { +// A utility for measuring time between readings as well as total elapsed time. +// The timer starts upon construction, but can be restarted. class Timer { - std::string name; std::chrono::steady_clock::time_point startTime; - double total = 0; + std::chrono::steady_clock::time_point lastTime; public: - Timer(std::string name = "") : name(name) {} + Timer() { restart(); } - void start() { startTime = std::chrono::steady_clock::now(); } + // Resets the timer's start time and returns the time since its last start. + void restart() { + auto now = std::chrono::steady_clock::now(); + lastTime = startTime = now; + } - void stop() { - total += std::chrono::duration(std::chrono::steady_clock::now() - - startTime) - .count(); + // The time since the timer was started. + double totalElapsed() { + auto now = std::chrono::steady_clock::now(); + auto sinceStart = std::chrono::duration(now - startTime).count(); + return sinceStart; } - double getTotal() { return total; } + // The time since the last `lastElapsed` measurement, if any, or since the + // timer started. + double lastElapsed() { + auto now = std::chrono::steady_clock::now(); + auto sinceLast = std::chrono::duration(now - lastTime).count(); + lastTime = now; + return sinceLast; + } - void dump() { std::cerr << "\n"; } + // The time elapsed since the last measurement and the total elapsed time. + std::pair elapsed() { + auto now = std::chrono::steady_clock::now(); + auto sinceLast = std::chrono::duration(now - lastTime).count(); + auto sinceStart = std::chrono::duration(now - startTime).count(); + lastTime = now; + return {sinceLast, sinceStart}; + } }; } // namespace wasm diff --git a/src/tools/wasm-reduce/wasm-reduce.cpp b/src/tools/wasm-reduce/wasm-reduce.cpp index 79696d6a7ba..fd6f3b8e3cd 100644 --- a/src/tools/wasm-reduce/wasm-reduce.cpp +++ b/src/tools/wasm-reduce/wasm-reduce.cpp @@ -97,7 +97,6 @@ struct ProgramResult { #ifdef _WIN32 void getFromExecution(std::string command) { Timer timer; - timer.start(); SECURITY_ATTRIBUTES saAttr; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; @@ -176,15 +175,13 @@ struct ProgramResult { output.append(chBuf); } } - timer.stop(); - time = timer.getTotal(); + time = timer.totalElapsed(); } #else // POSIX // runs the command and notes the output // TODO: also stderr, not just stdout? void getFromExecution(std::string command) { Timer timer; - timer.start(); const int MAX_BUFFER = 1024; char buffer[MAX_BUFFER]; FILE* stream = popen( @@ -195,8 +192,7 @@ struct ProgramResult { output.append(buffer); } code = pclose(stream); - timer.stop(); - time = timer.getTotal(); + time = timer.totalElapsed(); } #endif // _WIN32