parseInt
Dal punto di vista delle prestazioni e simili sono molto peggiori rispetto ad altre soluzioni, perché almeno richiedono una gestione delle eccezioni.
Ho eseguito i test jmh e ho scoperto che iterare su String usando charAt
e confrontando i caratteri con i caratteri al contorno è il modo più veloce per verificare se la stringa contiene solo cifre.
Test JMH
I test confrontano le prestazioni di Character.isDigit
vs Pattern.matcher().matches
vs Long.parseLong
vs il controllo dei valori dei caratteri.
Questi modi possono produrre risultati diversi per stringhe non ascii e stringhe contenenti segni +/-.
I test vengono eseguiti in modalità Throughput ( maggiore è meglio ) con 5 iterazioni di riscaldamento e 5 iterazioni di test.
risultati
Si noti che parseLong
è quasi 100 volte più lento rispetto isDigit
al primo carico di prova.
## Test load with 25% valid strings (75% strings contain non-digit symbols)
Benchmark Mode Cnt Score Error Units
testIsDigit thrpt 5 9.275 ± 2.348 ops/s
testPattern thrpt 5 2.135 ± 0.697 ops/s
testParseLong thrpt 5 0.166 ± 0.021 ops/s
## Test load with 50% valid strings (50% strings contain non-digit symbols)
Benchmark Mode Cnt Score Error Units
testCharBetween thrpt 5 16.773 ± 0.401 ops/s
testCharAtIsDigit thrpt 5 8.917 ± 0.767 ops/s
testCharArrayIsDigit thrpt 5 6.553 ± 0.425 ops/s
testPattern thrpt 5 1.287 ± 0.057 ops/s
testIntStreamCodes thrpt 5 0.966 ± 0.051 ops/s
testParseLong thrpt 5 0.174 ± 0.013 ops/s
testParseInt thrpt 5 0.078 ± 0.001 ops/s
Suite di test
@State(Scope.Benchmark)
public class StringIsNumberBenchmark {
private static final long CYCLES = 1_000_000L;
private static final String[] STRINGS = {"12345678901","98765432177","58745896328","35741596328", "123456789a1", "1a345678901", "1234567890 "};
private static final Pattern PATTERN = Pattern.compile("\\d+");
@Benchmark
public void testPattern() {
for (int i = 0; i < CYCLES; i++) {
for (String s : STRINGS) {
boolean b = false;
b = PATTERN.matcher(s).matches();
}
}
}
@Benchmark
public void testParseLong() {
for (int i = 0; i < CYCLES; i++) {
for (String s : STRINGS) {
boolean b = false;
try {
Long.parseLong(s);
b = true;
} catch (NumberFormatException e) {
// no-op
}
}
}
}
@Benchmark
public void testCharArrayIsDigit() {
for (int i = 0; i < CYCLES; i++) {
for (String s : STRINGS) {
boolean b = false;
for (char c : s.toCharArray()) {
b = Character.isDigit(c);
if (!b) {
break;
}
}
}
}
}
@Benchmark
public void testCharAtIsDigit() {
for (int i = 0; i < CYCLES; i++) {
for (String s : STRINGS) {
boolean b = false;
for (int j = 0; j < s.length(); j++) {
b = Character.isDigit(s.charAt(j));
if (!b) {
break;
}
}
}
}
}
@Benchmark
public void testIntStreamCodes() {
for (int i = 0; i < CYCLES; i++) {
for (String s : STRINGS) {
boolean b = false;
b = s.chars().allMatch(c -> c > 47 && c < 58);
}
}
}
@Benchmark
public void testCharBetween() {
for (int i = 0; i < CYCLES; i++) {
for (String s : STRINGS) {
boolean b = false;
for (int j = 0; j < s.length(); j++) {
char charr = s.charAt(j);
b = '0' <= charr && charr <= '9';
if (!b) {
break;
}
}
}
}
}
}
Aggiornato il 23 febbraio 2018
- Aggiungi altri due casi: uno usando
charAt
invece di creare array extra e un altro usando i IntStream
codici char
- Aggiungi l'interruzione immediata se non viene trovata una cifra per i casi di test in loop
- Restituisce false per stringa vuota per casi di test in loop
Aggiornato il 23 febbraio 2018
- Aggiungi un altro test case (il più veloce!) Che confronta il valore char senza usare lo stream
matches("\\d{2,}")
o prova con unPattern
eMatcher