Note that there are some explanatory texts on larger screens.

plurals
  1. POBenchmark of Java Try/Catch Block
    primarykey
    data
    text
    <p>I know that going into a catch block has some significance cost when executing a program, however, I was wondering if entering a try{} block also had any impact so I started looking for an answer in google with many opinions, but no benchmarking at all. Some answers I found were:</p> <ol> <li><a href="https://stackoverflow.com/questions/4280831/java-try-catch-performance-is-it-recommended-to-keep-what-is-inside-the-try-cla">Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?</a></li> <li><a href="https://stackoverflow.com/questions/8255878/try-catch-performance-java">Try Catch Performance Java</a></li> <li><a href="https://stackoverflow.com/questions/5158665/java-try-catch-blocks">Java try catch blocks</a></li> </ol> <p>However they didn't answer my question with facts, so I decided to try it for myself.</p> <p>Here's what I did. I have a csv file with this format:</p> <p><code>host;ip;number;date;status;email;uid;name;lastname;promo_code;</code></p> <p>where everything after status is optional and will not even have the corresponding ; , so when parsing a validation has to be done to see if the value is there, here's where the try/catch issue came to my mind.</p> <p>The current code that I inherited in my company does this:</p> <pre><code>StringTokenizer st=new StringTokenizer(line,";"); String host = st.nextToken(); String ip = st.nextToken(); String number = st.nextToken(); String date = st.nextToken(); String status = st.nextToken(); String email = ""; try{ email = st.nextToken(); }catch(NoSuchElementException e){ email = ""; } </code></pre> <p>and it repeats what it's done for email with uid, name, lastname and promo_code.</p> <p>and I changed everything to:</p> <pre><code>if(st.hasMoreTokens()){ email = st.nextToken(); } </code></pre> <p>and in fact it performs faster. When parsing a file that doesn't have the optional columns. Here are the average times:</p> <pre><code> --- Trying:122 milliseconds --- Checking:33 milliseconds </code></pre> <p>however, here's what confused me and the reason I'm asking: When running the example with values for the optional columns in all 8000 lines of the CSV, the if() version still performs better than the try/catch version, so my question is</p> <p><strong>Does really the try block does not have any performance impact on my code?</strong></p> <p>The average times for this example are:</p> <pre><code>--- Trying:105 milliseconds --- Checking:43 milliseconds </code></pre> <p>Can somebody explain what's going on here?</p> <p>Thanks a lot</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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