PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/bench/kilim/bench/Stopwatch.java

http://github.com/kilim/kilim
Java | 63 lines | 37 code | 14 blank | 12 comment | 2 complexity | 34380f4ba770ec9d5b1f97a6b8cc9dcc MD5 | raw file
 1/* Copyright (c) 2006, Sriram Srinivasan
 2 *
 3 * You may distribute this software under the terms of the license 
 4 * specified in the file "License"
 5 */
 6
 7package kilim.bench;
 8
 9public class Stopwatch {
10    long lastTickMillis = System.currentTimeMillis();
11    long lastElapsedMillis = 0;
12    int  multiplier = 1; // see reportMicros
13    String  unit = " ms";
14    String name = "";
15    
16    
17    public Stopwatch() {tick();}
18    public Stopwatch(String nm) {name = nm; tick();}
19    
20    /**
21     * @return the diff in millis from the last tick. Both tick and elapsed times are 
22     * kept in millis
23     */
24    public long tick() {
25        long l = lastTickMillis;
26        lastTickMillis = System.currentTimeMillis();
27        lastElapsedMillis = lastTickMillis - l;
28        return lastElapsedMillis;
29    }
30    
31    /**
32     * Report in micros or millis 
33     */
34    public void reportMicros() {multiplier = 1000; unit = " micros";}
35    public void reportMillis() {multiplier = 1; unit = " ms";}
36
37    public long timePerIter(int iters) {
38        return iters == 0? 0 : lastElapsedMillis*multiplier/iters;
39    }
40    
41    long itersPerTime(int iters) {
42        return lastElapsedMillis == 0 ? iters : iters/(lastElapsedMillis*multiplier);
43    }
44    
45    public String toString() {
46        return name + ": elapsed: " + (lastElapsedMillis * multiplier) + " " + unit;
47    }
48    
49    public String toString(int iters) {
50        return name + " elapsed: " + (lastElapsedMillis * multiplier) + 
51            unit +", iters = " + iters + 
52            ", " + unit + "/iter = " + timePerIter(iters) + 
53            ", iters/" + unit + " = " + itersPerTime(iters);
54    }
55    
56    public void print() {System.out.println(this);}
57    
58    public void print(int iters) { System.out.println(this.toString(iters));}
59
60    public void tickPrint() {tick(); print();}
61    
62    public void tickPrint(int iters) {tick(); print(iters);}
63}