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

/bench/kilim/bench/Ex_vs_Ret.java

http://github.com/kilim/kilim
Java | 65 lines | 46 code | 10 blank | 9 comment | 8 complexity | b9f34f2f1450047e996476ad81e85813 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 Ex_vs_Ret {
10
11    /**
12     * @param args
13     */
14    public static void main(String[] args) {
15        final int ntimes = 1000000;
16        final int depth = 10;
17
18        // JIT Warmup ===========================================
19        for (int i = 0; i < 1000; i++) ret(depth);
20        for (int i = 0; i < 1000; i++) {
21            try {
22                ex(depth);
23            } catch (FastEx ignore) {}
24        }
25
26        long start = System.currentTimeMillis();
27        for (int i = 0; i < ntimes; i++) {
28            ret(depth);
29        }
30        long elapsed = System.currentTimeMillis() - start;
31        System.out.println("Iterations = : " +ntimes + ", stack depth = " + depth);
32        System.out.println("ret ms: " + elapsed);
33
34        start = System.currentTimeMillis();
35        for (int i = 0; i < ntimes; i++) {
36            try {
37                ex(depth);
38            } catch (FastEx fe) {}
39        }
40        elapsed = System.currentTimeMillis() - start;
41        System.out.println("ex : " + elapsed);
42    }
43    
44    static void ret(int depth) {
45        if (depth != 0) {
46            ret(depth-1);
47        }
48    }
49    
50    static void ex(int depth) throws FastEx {
51        if (depth == 0) {
52            throw new FastEx();
53        }
54        ex(depth-1);
55    }
56}
57
58final class FastEx extends Throwable {
59    private static final long serialVersionUID = 1L; // Just suppressing warnings.
60
61    @Override
62    public synchronized Throwable fillInStackTrace() {
63        return null; // The most time consuming part of throwing an exception.
64    }
65}