Skip to content

Instantly share code, notes, and snippets.

@zeroum
Last active December 11, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroum/4522146 to your computer and use it in GitHub Desktop.
Save zeroum/4522146 to your computer and use it in GitHub Desktop.
Um teste de performance de parse de string para número de 1-65535 usando NumberFormatException ou regular expressions para validar os números.
import java.util.Random;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
int TAMANHO = 10 * 250 * 1000;
Pattern p = Pattern.compile("^[0-9]{1,6}$");
for (int j = 0; j < 25; j++) {
System.gc();
int notNum1 = 0;
int notNum2 = 0;
String[] array = new String[TAMANHO];
Random rand = new Random();
for (int i = 0; i < array.length; i++) {
if (rand.nextInt((int) Math.pow(2, 16)) < Math.pow(2, 15))
array[i] = Integer.toString(rand.nextInt(65536 * 2));
else
array[i] = Integer.toString(rand.nextInt(65536 * 2), 16);
}
int[] array2;
long t1, t2, t3, t4;
System.gc();
array2 = new int[TAMANHO];
t1 = System.nanoTime();
for (int i = 0; i < array.length; i++) {
String s = array[i];
if (p.matcher(s).matches()) {
int num = Integer.parseInt(s);
if (num >= 1 && num <= 65535)
array2[i] = num;
} else
notNum2++;
}
t2 = System.nanoTime();
array2 = null;
System.gc();
array2 = new int[TAMANHO];
t3 = System.nanoTime();
for (int i = 0; i < array.length; i++) {
String s = array[i];
try {
int num = Integer.parseInt(s);
if (num >= 1 && num <= 65535)
array2[i] = num;
} catch (NumberFormatException e) {
notNum1++;
}
}
t4 = System.nanoTime();
double pps = (TAMANHO * 1000000000.0 / (t2 - t1));
System.out.printf("Duração: %dns (%.0f parses/s)[1 parse em %.9fs]\n", t2-t1, pps, 1/pps);
pps = (TAMANHO * 1000000000.0 / (t4 - t3));
System.out.printf("Duração: %dns (%.0f parses/s)[1 parse em %.9fs]\n", t4-t3, pps, 1/pps);
System.out.printf("Diff : %dns (%.2f%%)\n", (t4-t3)-(t2-t1), ((t4-t3)*100.0/(t2-t1))-100);
System.out.printf("notNum1 = %d\nnotNum2 = %d\n\n", notNum1,
notNum2);
}
}
}
@zeroum
Copy link
Author

zeroum commented Jan 13, 2013

Regex compilado previamente tem ganho de 220% a 230% em cima da solução com exceções. Isso significou 333ns no melhor caso.

Regex compilado a cada iteração por sua vez, apresentaram de 20 a 50%, apenas 50 a 150ns de ganho, dependendo do conjunto do trabalho, se a cpu estava fria, e número de iterações.

Não foi verificado o uso de memória de cada solução. Alguém sabe como medir isso?

xxxx
java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
xxxx
Model Name: MacBook Pro
Model Identifier: MacBookPro8,2
Processor Name: Intel Core i7
Processor Speed: 2,2 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Memory: 8 GB
xxxxx

Top 5

Duração: 372232000ns (6716241 parses/s)[1 parse em 0.000000149s]
Duração: 1193983000ns (2093832 parses/s)[1 parse em 0.000000478s]
Diff : 821751000ns (220.76%)

Duração: 368762000ns (6779440 parses/s)[1 parse em 0.000000148s]
Duração: 1188933000ns (2102726 parses/s)[1 parse em 0.000000476s]
Diff : 820171000ns (222.41%)

Duração: 362962000ns (6887773 parses/s)[1 parse em 0.000000145s]
Duração: 1201784000ns (2080241 parses/s)[1 parse em 0.000000481s]
Diff : 838822000ns (231.10%)

Duração: 371052000ns (6737600 parses/s)[1 parse em 0.000000148s]
Duração: 1192589000ns (2096280 parses/s)[1 parse em 0.000000477s]
Diff : 821537000ns (221.41%)

Duração: 359991000ns (6944618 parses/s)[1 parse em 0.000000144s]
Duração: 1193241000ns (2095134 parses/s)[1 parse em 0.000000477s]
Diff : 833250000ns (231.46%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment