From: <log...@gm...> - 2009-04-01 20:13:33
|
package error_handler_benchmark; public class BenchCatchThrow { static int coolDown; static void type_error_handler(BaseObject obj) { // cooldown; required so its not eliminated by JIT coolDown++; } static class BaseObject { public long longValue() { type_error_handler(this); return 0; } } static class RightObject extends BaseObject { public final long value = 666; @Override public long longValue() { return value; } } static class WrongObject extends BaseObject { } public static void main(String[] args) throws Exception { System.out.println("java.vm.vendor " + System.getProperty("java.vm.vendor")); System.out .println("java.version " + System.getProperty("java.version")); System.out.println("java.vm.version " + System.getProperty("java.vm.version")); for (int i = 1; i <= 10; i++) { System.out.println(); System.out.println("Run " + i); mainBench(); } } public static void mainBench() throws Exception { long m1Time = 0; long m2Time = 0; long m3Time = 0; for (int i = 0; i < 10; i++) { coolDown = 0; m1Time += benchM1(); coolDown = 0; m2Time += benchM2(); coolDown = 0; m3Time += benchM3(); } System.out.println("Exception catching time = " + m1Time + "ms"); System.out.println("Exception avoiding time = " + m3Time + "ms"); System.out.println("Deterministic call time = " + m2Time + "ms"); System.out.println("% = " + ((double) m2Time / (double) m1Time) * 100); } static BaseObject GOOD = new RightObject(); static BaseObject BAD = new WrongObject(); static final int ITERATIONS = 100000; private static long benchM1() { long start = System.currentTimeMillis(); long count = 0; for (int i = ITERATIONS; i > 0; i--) { BaseObject arg = i % 2 == 0 ? GOOD : BAD; try { count += ((RightObject) arg).value; } catch (ClassCastException cce) { type_error_handler(arg); } } long end = System.currentTimeMillis(); return (end - start); } private static long benchM2() { long start = System.currentTimeMillis(); int count = 0; for (int i = ITERATIONS; i > 0; i--) { BaseObject arg = i % 2 == 0 ? GOOD : BAD; count += arg.longValue(); } long end = System.currentTimeMillis(); return (end - start); } private static long benchM3() { long start = System.currentTimeMillis(); int count = 0; for (int i = ITERATIONS; i > 0; i--) { BaseObject arg = i % 2 == 0 ? GOOD : BAD; if (arg instanceof RightObject) count += ((RightObject) arg).value; else type_error_handler(arg); } long end = System.currentTimeMillis(); return (end - start); } } OUTPUT: java.vm.vendor Sun Microsystems Inc. java.version 1.6.0_10 java.vm.version 11.0-b15 Run 1 Exception catching time = 5821ms Exception avoiding time = 9ms Deterministic call time = 5ms % = 0.08589589417625838 Run 2 Exception catching time = 5413ms Exception avoiding time = 3ms Deterministic call time = 2ms % = 0.03694808793644929 Run 3 Exception catching time = 5568ms Exception avoiding time = 2ms Deterministic call time = 4ms % = 0.07183908045977011 Run 4 Exception catching time = 5464ms Exception avoiding time = 3ms Deterministic call time = 1ms % = 0.018301610541727673 Run 5 Exception catching time = 5480ms Exception avoiding time = 5ms Deterministic call time = 3ms % = 0.05474452554744526 Run 6 Exception catching time = 5626ms Exception avoiding time = 3ms Deterministic call time = 5ms % = 0.08887308922858159 Run 7 Exception catching time = 5638ms Exception avoiding time = 2ms Deterministic call time = 1ms % = 0.0177367860943597 Run 8 Exception catching time = 5579ms Exception avoiding time = 3ms Deterministic call time = 2ms % = 0.0358487184083169 Run 9 Exception catching time = 5603ms Exception avoiding time = 3ms Deterministic call time = 2ms % = 0.03569516330537212 Run 10 Exception catching time = 5434ms Exception avoiding time = 5ms Deterministic call time = 1ms % = 0.01840264998159735 |