Solo per divertimento, ho eseguito questo micro punto di riferimento. I risultati dell'ultima analisi (es.alberino JVM riscaldare/JIT) sono inferiori (risultati sono abbastanza consistente da una corsa all'altra comunque):
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 altre parole, caratteri è molto efficiente, Integer.parseInt è efficiente come char se la stringa è un numero, ma terribilmente lento se la stringa non è un numero. Regex è nel mezzo.
Conclusione
Se si analizza una stringa in un numero e ci si aspetta la stringa da un numero, in generale, utilizzando Integer.parseInt è la soluzione migliore (efficiente e leggibile). La penalità che si ottiene quando la stringa non è un numero dovrebbe essere bassa se non è troppo frequente.
ps: la mia espressione regolare forse non è ottimale, non esitate a commentare.
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;
}
}
Un modo semplice per scoprirlo: esegui entrambe le opzioni e il tempo ciascuna. Il processo è vincolato alla CPU, quindi la durata con indica quale è più efficiente. Si noti che è possibile rendere più efficiente la regex riutilizzando il pattern compilato, piuttosto che usando 'string.matches()', che ricompila la regex ogni chiamata. – Bohemian