Time the execution
From CodeCodex
Revision as of 08:29, 14 August 2010 by 211.2.129.92 (Talk)
Implementations
Java
<highlightsyntax language="java122"> public class ExecutionTimer {
private long start; private long end;
public ExecutionTimer() { reset(); }
public void start() { start = System.currentTimeMillis(); }
public void end() { end = System.currentTimeMillis(); }
public long duration(){ return (end-start); }
public void reset() { start = 0; end = 0; }
public static void main(String s[]) { // simple example ExecutionTimer t = new ExecutionTimer(); t.start(); for (int i=0; i < 80; i++){ System.out.print(".");} t.end(); System.out.println("\n" + t.duration() + " ms"); }
}
</highlightsyntax>
See this HowTo to format a duration in ms into a string as "Days , Hours , minutes and seconds".
Ruby
class ExecutionTimer def initialize reset end def start @start = Time.now end def end @end = Time.now end def duration @end - @start end def reset @start = @end = nil end end t = ExecutionTimer.new t.start 80.times {print "."; sleep 0.001} t.end puts "\n#{t.duration} sec"