1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author user
*
*/
public class Snippet
{
String sentence = "untroisdeuxunununquatreneuftroisdeuxununcinquncinqquatre";
public static void main(String args[]) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
new Snippet();
}
public Snippet() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
bench_(Snippet.class.getMethod("method_1", String.class));
bench_(Snippet.class.getMethod("method_2", String.class));
bench_(Snippet.class.getMethod("method_1", String.class));
bench_(Snippet.class.getMethod("method_2", String.class));
bench_(Snippet.class.getMethod("method_2", String.class));
bench_(Snippet.class.getMethod("method_1", String.class));
}
/**
* @param method
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
private void bench_(Method method) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
long start = System.currentTimeMillis();
for (int i = 0; i < 50000; i++)
{
String retour = (String) method.invoke(this, sentence);
//System.err.println("Retour = "+retour.toString());
}
long stop = System.currentTimeMillis();
long elapsed = stop-start;
System.err.println("Elapsed for method "+method.getName()+ " : "+elapsed+ " ms");
}
public String method_1(String sentence)
{
String vars [] = {"un","deux","trois","quatre", "cinq", "six", "sept", "huit", "neuf"};
String vars2 [] = {"1","2","3","4", "5", "6", "7", "8", "9"};
String finalSent = "";
for (int i = 0; i < sentence.length(); i++) {
for (int j = 0; j < vars.length; j++) {
if (sentence.substring(i).startsWith(vars[j])==true)
{
//System.err.println("-->"+vars[j]);
finalSent+=vars2[j]+";";
i+=vars[j].length()-1;
}
}
}
return finalSent;
}
public String method_2(String sentence)
{
String vars1 [] = {"un","deux","trois","quatre", "cinq", "six", "sept", "huit", "neuf"};
String vars2 [] = {"1","2","3","4", "5", "6", "7", "8", "9"};
for (int i = 0; i < vars2.length; i++)
{
sentence=sentence.replace(vars1[i], vars2[i]);
}
return sentence;
}
} |
Partager