From f1015634c6dbb7e2fa92b17f1298a1587d7d6fb9 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Thu, 8 Jan 2026 14:32:21 -0800 Subject: [PATCH] [NFC] Update the timer utility Reduce verbosity by implicitly starting the timer when it is first constructed. Separate concerns by removing printing functionality. Add the ability to measure time elapsed since the last measurement to better support the use case of measuring several subsequent steps of an operation as well as the total elapsed time. --- src/support/timing.h | 47 ++++++++++++++++++--------- src/tools/wasm-reduce/wasm-reduce.cpp | 8 ++--- 2 files changed, 33 insertions(+), 22 deletions(-) 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