Skip to content

Instantly share code, notes, and snippets.

@besstiolle
Last active December 31, 2015 20: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 besstiolle/8038837 to your computer and use it in GitHub Desktop.
Save besstiolle/8038837 to your computer and use it in GitHub Desktop.
Testons les différentes manières de détecter si une chaine de caractère est un entier positif. Exemple également avec un entier positif ou négatif + d'info sur http://www.furie.be/news/48/15/Comment-tester-un-numerique-dans-Java.html
package be.furie.example;
import java.util.regex.Pattern;
public class testNumber {
private static final Pattern PATTERN_INTEGER = Pattern.compile("[0-9]+");
private static final int NUM_ELT = 1000000;
private static String[] gen = new String[NUM_ELT];
private static void initGenerateInt(int ratioLetter) {
String car = "";
for (int i = 0; i < NUM_ELT; i++) {
car = "";
if (i % ratioLetter == 0) {
car = "a";
}
gen[i] = "" + Math.round(Math.random() * 100 - 50) + car;
}
}
private static boolean isIntegerByBytes(String val) {
byte[] bytes = val.getBytes();
for (int i = 0; i < bytes.length; i++) {
if (!Character.isDigit((char) bytes[i])) {
return false;
}
}
return true;
}
private static boolean isIntegerRegEx(String val) {
return PATTERN_INTEGER.matcher(val).matches();
}
private static boolean isIntegerException(String val) {
try {
Integer.valueOf(val);
return !val.startsWith("-");
} catch (NumberFormatException e) {
return false;
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("TEST avec zero lettre ");
run(1000000);
System.out.println("TEST avec 1 lettre par 1000 lignes");
run(1000);
System.out.println("TEST avec 1 lettre par 10 lignes");
run(10);
System.out.println("TEST avec 1 lettre par 5 lignes");
run(5);
System.out.println("TEST avec 1 lettre par 4 lignes");
run(4);
System.out.println("TEST avec 1 lettre par 3 lignes");
run(3);
System.out.println("TEST avec 1 lettre par 2 lignes");
run(2);
System.out.println("TEST avec 1 lettre par ligne");
run(1);
}
private static void run(int numElt) {
initGenerateInt(numElt);
long start = System.currentTimeMillis();
int cpt = 0;
for (int i = 0; i < NUM_ELT; i++) {
if (isIntegerRegEx(gen[i])) {
cpt++;
}
}
System.out.println(" > RegEx : " + (System.currentTimeMillis() - start) + " ms avec " + cpt + " nombres positif");
start = System.currentTimeMillis();
cpt = 0;
for (int i = 0; i < NUM_ELT; i++) {
if (isIntegerException(gen[i])) {
cpt++;
}
}
System.out.println(" > Exception : " + (System.currentTimeMillis() - start) + " ms avec " + cpt + " nombres positif");
start = System.currentTimeMillis();
cpt = 0;
for (int i = 0; i < NUM_ELT; i++) {
if (isIntegerByBytes(gen[i])) {
cpt++;
}
}
System.out.println(" > ByBytes : " + (System.currentTimeMillis() - start) + " ms avec " + cpt + " nombres positif");
}
}
package be.furie.example;
import java.util.regex.Pattern;
public class testNumber2 {
private static final Pattern PATTERN_INTEGER = Pattern.compile("\\-?[0-9]+");
private static final int NUM_ELT = 1000000;
private static String[] gen = new String[NUM_ELT];
private static void initGenerateInt(int ratioLetter) {
String car = "";
for (int i = 0; i < NUM_ELT; i++) {
car = "";
if (i % ratioLetter == 0) {
car = "a";
}
gen[i] = "" + Math.round(Math.random() * 100 - 50) + car;
}
}
// http://www.developpez.net/forums/d483247/java/general-java/langage/verifier-qu-nombre-entier-java/#post2907422
private static boolean isIntegerByBytes(String val) {
byte[] bytes = val.getBytes();
for (int i = 0; i < bytes.length; i++) {
if (!Character.isDigit((char) bytes[i])) {
if (i == 0 && '-' == (char) bytes[i]) {
continue;
}
return false;
}
}
return true;
}
private static boolean isIntegerRegEx(String val) {
return PATTERN_INTEGER.matcher(val).matches();
}
private static boolean isIntegerException(String val) {
try {
Integer.valueOf(val);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("TEST avec zero lettre ");
run(1000000);
System.out.println("TEST avec 1 lettre par 1000 lignes");
run(1000);
System.out.println("TEST avec 1 lettre par 10 lignes");
run(10);
System.out.println("TEST avec 1 lettre par 5 lignes");
run(5);
System.out.println("TEST avec 1 lettre par 4 lignes");
run(4);
System.out.println("TEST avec 1 lettre par 3 lignes");
run(3);
System.out.println("TEST avec 1 lettre par 2 lignes");
run(2);
System.out.println("TEST avec 1 lettre par ligne");
run(1);
}
private static void run(int numElt) {
initGenerateInt(numElt);
long start = System.currentTimeMillis();
int cpt = 0;
for (int i = 0; i < NUM_ELT; i++) {
if (isIntegerRegEx(gen[i])) {
cpt++;
}
}
System.out.println(" > RegEx : " + (System.currentTimeMillis() - start) + " ms avec " + cpt + " nombres positif");
start = System.currentTimeMillis();
cpt = 0;
for (int i = 0; i < NUM_ELT; i++) {
if (isIntegerException(gen[i])) {
cpt++;
}
}
System.out.println(" > Exception : " + (System.currentTimeMillis() - start) + " ms avec " + cpt + " nombres positif");
start = System.currentTimeMillis();
cpt = 0;
for (int i = 0; i < NUM_ELT; i++) {
if (isIntegerByBytes(gen[i])) {
cpt++;
}
}
System.out.println(" > ByBytes : " + (System.currentTimeMillis() - start) + " ms avec " + cpt + " nombres positif");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment