Just for fun, I ran this micro test. The results of the last run (i.e., Post-JVM warm-up / JIT) are lower (the results in any case are quite consistent from one run to another):
regex with numbers 123 chars with numbers 33 parseInt with numbers 33 regex with words 123 chars with words 34 parseInt with words 733
In other words, characters are very efficient, Integer.parseInt is as efficient as char IF a string is a number, but terribly slow if the string is not a number. Regex is in between.
Conclusion
If you parse a string into a number and you expect the string to be the whole number, using Integer.parseInt is the best solution (efficient and readable). The penalty you get when a string is not a number should be low if it is not too frequent.
ps: my regex may not be optimal, feel free to comment.
public class TestNumber { private final static List<String> numbers = new ArrayList<>(); private final static List<String> words = new ArrayList<>(); public static void main(String args[]) { long start, end; Random random = new Random(); for (int i = 0; i < 1000000; i++) { numbers.add(String.valueOf(i)); words.add(String.valueOf(i) + "x"); } for (int i = 0; i < 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<String> 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<String> list) { int sum = 0; for (String s : list) { boolean isNumber = true; for (char c : s.toCharArray()) { if (c < '0' || c > '9') { isNumber = false; break; } } if (isNumber) { sum++; } } return sum; } private static int exception(List<String> list) { int sum = 0; for (String s : list) { try { Integer.parseInt(s); sum++; } catch (NumberFormatException e) { } } return sum; } }
assylias
source share