From d09fc511e24ef6416b026441e68269a7189c0328 Mon Sep 17 00:00:00 2001 From: Max Schaefer Date: Tue, 20 Nov 2012 10:45:56 -0500 Subject: [PATCH] Added a logging stopwatch for debugging. --- .../ibm/wala/util/debug/LoggingStopwatch.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 com.ibm.wala.util/src/com/ibm/wala/util/debug/LoggingStopwatch.java diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/debug/LoggingStopwatch.java b/com.ibm.wala.util/src/com/ibm/wala/util/debug/LoggingStopwatch.java new file mode 100644 index 000000000..401f2d0ff --- /dev/null +++ b/com.ibm.wala.util/src/com/ibm/wala/util/debug/LoggingStopwatch.java @@ -0,0 +1,43 @@ +package com.ibm.wala.util.debug; + +/** + * A stop watch that prints log messages. + * + * @author mschaefer + * + */ +public class LoggingStopwatch { + private long start; + + /** + * Start the stopwatch. + */ + public void start() { + this.start = System.nanoTime(); + } + + /** + * Mark the completion of a task, print the time it took to complete, + * and optionally restart the stopwatch. + * + * @param msg message to print + * @param reset whether to restart the stopwatch + * @return the elapsed time in milliseconds + */ + public long mark(String msg, boolean reset) { + long end = System.nanoTime(); + long elapsed = (end-start)/1000000; + System.out.println(msg + ": " + elapsed + "ms"); + if(reset) + start(); + return elapsed; + } + + /** + * Convenience method that invokes {@link #mark(String, boolean)} with {@code true} + * as its second argument. + */ + public long mark(String msg) { + return mark(msg, true); + } +}