Time the execution
From CodeCodex
Revision as of 01:46, 8 March 2011 by 211.2.129.92 (Talk)
Implementations[edit]
Java[edit]
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"); } }
See this HowTo to format a duration in ms into a string as "Days , Hours , minutes and seconds".
Ruby[edit]
class ExecutionTimer def initialize reset end def start @start = Time.now @end = nil end def end @end = Time.now end def duration @end - @start rescue raise "#{self.class} setup error (start or end)" end def reset @start = @end = nil end end t = ExecutionTimer.new t.start 80.times {print "."; sleep 0.01} t.end puts "\n#{t.duration} sec"