Created
March 17, 2016 13:48
-
-
Save alexradzin/3e9c18cd116706212fa0 to your computer and use it in GitHub Desktop.
try/catch/finally performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TryPerf { | |
private static int foo(int a) { | |
return a * 2; | |
} | |
private static int tryFinally(int a) { | |
try { | |
return a * 2; | |
} finally { | |
// do nothing | |
} | |
} | |
private static int tryCatchFinally(int a) { | |
try { | |
return a * 2; | |
} catch (RuntimeException e) { | |
throw new RuntimeException(e); | |
} finally { | |
// do nothing | |
} | |
} | |
private static int tryThrowCatch(int a) { | |
try { | |
return a / 0; | |
} catch (RuntimeException e) { | |
return -1; | |
} finally { | |
// do nothing | |
} | |
} | |
private static int tryThrowCatch1(int a) { | |
try { | |
try { | |
return a / 0; | |
} catch (RuntimeException e) { | |
throw e; | |
} finally { | |
// do nothing | |
} | |
} catch (RuntimeException e2) { | |
// do nothing | |
return -1; | |
} | |
} | |
private static int tryThrowCatch2(int a) { | |
try { | |
try { | |
return a / 0; | |
} catch (RuntimeException e) { | |
throw new RuntimeException(e); | |
} finally { | |
// do nothing | |
} | |
} catch (RuntimeException e2) { | |
// do nothing | |
return -1; | |
} | |
} | |
private static int tryCatch(int a) { | |
try { | |
return a * 2; | |
} catch (RuntimeException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static void main(String[] args) { | |
int sum = 0; | |
long before = System.nanoTime(); | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += foo(i); | |
} | |
long after = System.nanoTime(); | |
System.out.println("foo: " + (after - before)/1_000_000 + " ms"); | |
sum = 0; | |
before = System.nanoTime(); | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += tryCatch(i); | |
} | |
after = System.nanoTime(); | |
System.out.println("tryCatch: " + (after - before)/1_000_000 + " ms"); | |
sum = 0; | |
before = System.nanoTime(); | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += tryFinally(i); | |
} | |
after = System.nanoTime(); | |
System.out.println("tryFinally: " + (after - before)/1_000_000 + " ms"); | |
sum = 0; | |
before = System.nanoTime(); | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += tryCatchFinally(i); | |
} | |
after = System.nanoTime(); | |
System.out.println("tryCatchFinally: " + (after - before)/1_000_000 + " ms"); | |
before = System.nanoTime(); | |
sum = 0; | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += tryThrowCatch(i); | |
} | |
after = System.nanoTime(); | |
System.out.println("tryThrowCatch: " + (after - before)/1_000_000 + " ms"); | |
before = System.nanoTime(); | |
sum = 0; | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += tryThrowCatch1(i); | |
} | |
after = System.nanoTime(); | |
System.out.println("tryThrowCatch1: " + (after - before)/1_000_000 + " ms"); | |
before = System.nanoTime(); | |
sum = 0; | |
for (int i = 0; i < 100_000_000; i++) { | |
sum += tryThrowCatch2(i); | |
} | |
after = System.nanoTime(); | |
System.out.println("tryThrowCatch2: " + (after - before)/1_000_000 + " ms"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment