Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All right, after <a href="https://stackoverflow.com/questions/141560/should-trycatch-go-inside-or-outside-a-loop#141652">Jeffrey L Whitledge said</a> that there was no performance difference (as of 1997), I went and tested it. I ran this small benchmark:</p> <pre><code>public class Main { private static final int NUM_TESTS = 100; private static int ITERATIONS = 1000000; // time counters private static long inTime = 0L; private static long aroundTime = 0L; public static void main(String[] args) { for (int i = 0; i &lt; NUM_TESTS; i++) { test(); ITERATIONS += 1; // so the tests don't always return the same number } System.out.println("Inside loop: " + (inTime/1000000.0) + " ms."); System.out.println("Around loop: " + (aroundTime/1000000.0) + " ms."); } public static void test() { aroundTime += testAround(); inTime += testIn(); } public static long testIn() { long start = System.nanoTime(); Integer i = tryInLoop(); long ret = System.nanoTime() - start; System.out.println(i); // don't optimize it away return ret; } public static long testAround() { long start = System.nanoTime(); Integer i = tryAroundLoop(); long ret = System.nanoTime() - start; System.out.println(i); // don't optimize it away return ret; } public static Integer tryInLoop() { int count = 0; for (int i = 0; i &lt; ITERATIONS; i++) { try { count = Integer.parseInt(Integer.toString(count)) + 1; } catch (NumberFormatException ex) { return null; } } return count; } public static Integer tryAroundLoop() { int count = 0; try { for (int i = 0; i &lt; ITERATIONS; i++) { count = Integer.parseInt(Integer.toString(count)) + 1; } return count; } catch (NumberFormatException ex) { return null; } } } </code></pre> <p>I checked the resulting bytecode using javap to make sure that nothing got inlined.</p> <p>The results showed that, assuming insignificant JIT optimizations, <strong>Jeffrey is correct</strong>; there is absolutely <strong>no performance difference on Java 6, Sun client VM</strong> (I did not have access to other versions). The total time difference is on the order of a few milliseconds over the entire test.</p> <p>Therefore, the only consideration is what looks cleanest. I find that the second way is ugly, so I will stick to either the first way or <a href="https://stackoverflow.com/questions/141560/should-trycatch-go-inside-or-outside-a-loop#141589">Ray Hayes's way</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload