Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just for fun, I have run this micro benchmark. The results of the last run (i.e. post JVM warm up / JIT) are below (results are fairly consistent from one run to another anyway):</p> <pre><code>regex with numbers 123 chars with numbers 33 parseInt with numbers 33 regex with words 123 chars with words 34 parseInt with words 733 </code></pre> <p>In other words, chars is very efficient, Integer.parseInt is as efficient as char IF the string is a number, but awfully slow if the string is not a number. Regex is in between.</p> <p><strong>Conclusion</strong></p> <p>If you parse a string into a number and you expect the string to be a number in general, using Integer.parseInt is the best solution (efficient and readable). The penalty you get when the string is not a number should be low if it is not too frequent.</p> <p>ps: my regex is maybe not optimal, feel free to comment.</p> <pre><code>public class TestNumber { private final static List&lt;String&gt; numbers = new ArrayList&lt;&gt;(); private final static List&lt;String&gt; words = new ArrayList&lt;&gt;(); public static void main(String args[]) { long start, end; Random random = new Random(); for (int i = 0; i &lt; 1000000; i++) { numbers.add(String.valueOf(i)); words.add(String.valueOf(i) + "x"); } for (int i = 0; i &lt; 5; i++) { start = System.nanoTime(); regex(numbers); System.out.println("regex with numbers " + (System.nanoTime() - start) / 1000000); start = System.nanoTime(); chars(numbers); System.out.println("chars with numbers " + (System.nanoTime() - start) / 1000000); start = System.nanoTime(); exception(numbers); System.out.println("exceptions with numbers " + (System.nanoTime() - start) / 1000000); start = System.nanoTime(); regex(words); System.out.println("regex with words " + (System.nanoTime() - start) / 1000000); start = System.nanoTime(); chars(words); System.out.println("chars with words " + (System.nanoTime() - start) / 1000000); start = System.nanoTime(); exception(words); System.out.println("exceptions with words " + (System.nanoTime() - start) / 1000000); } } private static int regex(List&lt;String&gt; list) { int sum = 0; Pattern p = Pattern.compile("[0-9]+"); for (String s : list) { sum += (p.matcher(s).matches() ? 1 : 0); } return sum; } private static int chars(List&lt;String&gt; list) { int sum = 0; for (String s : list) { boolean isNumber = true; for (char c : s.toCharArray()) { if (c &lt; '0' || c &gt; '9') { isNumber = false; break; } } if (isNumber) { sum++; } } return sum; } private static int exception(List&lt;String&gt; list) { int sum = 0; for (String s : list) { try { Integer.parseInt(s); sum++; } catch (NumberFormatException e) { } } return sum; } } </code></pre>
    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