Created
February 27, 2013 08:51
-
-
Save anonymous/5046416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
import java.lang.reflect.Method; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.lang.annotation.ElementType; | |
class Test_15105672 { | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
private @interface Test {}; | |
// Number of times to run all methods | |
private static final int NUM_OUTER = 3; | |
// Number of times to run each method | |
private static final int NUM_REPEAT = 10; | |
public static void main(String args[]) { | |
runTestReflection(); | |
} | |
@Test | |
public static String getDigits(String str) { | |
StringBuilder output = new StringBuilder(); | |
boolean hasDigit = false; | |
boolean leadingZero = true; | |
for (int i = 0; i < str.length() && output.length() < 10; i++) { | |
char currChar = str.charAt(i); | |
if ('0' <= currChar && currChar <= '9') { | |
hasDigit = true; | |
if (currChar != '0') { | |
output.append(currChar); | |
leadingZero = false; | |
} else if (!leadingZero) { // currChar == 0 | |
output.append(currChar); | |
} // Ignore leading zero | |
} | |
} | |
return !hasDigit ? "" : output.length() == 0 ? "0" : output.toString(); | |
} | |
@Test | |
public static String getDigits2(String str) { | |
String digitOnly = str.replaceAll("\\D+", ""); | |
String noLeadingZero = digitOnly.replaceFirst("^0+", ""); | |
return digitOnly.isEmpty() ? "" : | |
noLeadingZero.isEmpty() ? "0" : | |
noLeadingZero.substring(0, Math.min(noLeadingZero.length(), 10)); | |
} | |
@Test | |
public static String getDigits3_MarounMaroun(String str) { | |
StringBuilder sb = new StringBuilder(); | |
for(int i = 0; i < str.length(); i++) { | |
if (Character.isDigit(str.charAt(i))) | |
sb = sb.append(str.charAt(i)); | |
} | |
if (sb.length() == 0) { | |
return ""; | |
} else { | |
String result = sb.toString().replaceFirst("^0*", ""); //Remove leading zeros | |
return result.isEmpty() ? "0" : result.substring(0, Math.min(result.length(), 10)); | |
} | |
} | |
private static String regex; | |
static { | |
regex = "^[\\D0]*(\\d)\\D*+"; | |
String tail = "(\\d)?"; | |
for (int i = 0; i < 8; i++) { | |
tail = "(?:(\\d)\\D*+" + tail + ")?"; | |
} | |
regex += tail; | |
} | |
private static final Pattern p = Pattern.compile(regex); | |
@Test | |
public static String getDigits4_Kobi(String str) { | |
Matcher m = p.matcher(str); | |
StringBuilder sb = new StringBuilder(); | |
if (m.find()) { | |
for (int i = 1; i <= 10 && m.group(i) != null; i++) { | |
sb.append(m.group(i)); | |
} | |
} | |
return sb.toString(); | |
} | |
private static ArrayList<Method> getTestMethods() { | |
Class<?> klass = null; | |
try { | |
klass = Class.forName(Thread.currentThread().getStackTrace()[1].getClassName()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println("Something really bad happened. Bailling out..."); | |
System.exit(1); | |
} | |
Method[] methods = klass.getMethods(); | |
// System.out.println(klass); | |
// System.out.println(Arrays.toString(methods)); | |
ArrayList<Method> testMethods = new ArrayList<Method>(); | |
for (Method method: methods) { | |
if (method.isAnnotationPresent(Test.class)) { | |
testMethods.add(method); | |
} | |
} | |
// System.out.println(testMethods); | |
return testMethods; | |
} | |
// Give higher chance for tons of leading zero | |
private static final double LEADING_ZERO = 0.30; | |
private static final double NO_DIGIT = 0.25; | |
private static Random rand = new Random(); | |
// Number of strings to test on | |
private static final int NUM_INPUT = 10000; | |
// From 20 - 140 characters | |
private static final int LENGTH = 80; | |
private static final int LENGTH_FLUCTUATION = 60; | |
public static String[] generateInput() { | |
String output[] = new String[NUM_INPUT]; | |
for (int i = 0; i < NUM_INPUT; i++) { | |
StringBuilder s = new StringBuilder(); | |
int length = LENGTH - rand.nextInt(LENGTH_FLUCTUATION * 2 + 1) + LENGTH_FLUCTUATION; | |
if (rand.nextDouble() < NO_DIGIT) { | |
for (int j = 0; j < length; j++) { | |
char c = (char) (0x20 + rand.nextInt(0x50)); | |
if (c < '0' || c > '9') { | |
s.append(c); | |
} else { | |
j--; | |
} | |
} | |
assert(s.length() == length); | |
} else if (rand.nextDouble() < LEADING_ZERO) { | |
int zeroLength = length - 9 + rand.nextInt(10); | |
for (int j = 0; j < zeroLength; j++) { | |
char c = (char) (0x20 + rand.nextInt(0x50)); | |
if (c < '1' || c > '9') { | |
s.append(c); | |
} else { | |
j--; | |
} | |
} | |
for ( ; zeroLength < length; zeroLength++) { | |
s.append((char) (0x20 + rand.nextInt(0x50))); | |
} | |
assert(s.length() == length); | |
} else { | |
for (int j = 0; j < length; j++) { | |
s.append((char) (0x20 + rand.nextInt(0x50))); | |
} | |
assert(s.length() == length); | |
} | |
output[i] = s.toString(); | |
} | |
return output; | |
} | |
public static void runTestReflection() { | |
ArrayList<Method> methods = getTestMethods(); | |
for (int t = 0; t < NUM_OUTER; t++) { | |
String inputSet[] = generateInput(); | |
String compareSet[] = null; | |
for (Method method: methods) { | |
System.out.print(method.getName() + ": "); | |
if (compareSet == null) { | |
compareSet = new String[inputSet.length]; | |
try { | |
for (int i = 0; i < inputSet.length; i++) { | |
compareSet[i] = (String) method.invoke(null, inputSet[i]); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} else { | |
try { | |
for (int i = 0; i < inputSet.length; i++) { | |
String result = (String) method.invoke(null, inputSet[i]); | |
if (!compareSet[i].equals(result)) { | |
System.err.println(compareSet[i] + " != " + result + " for input " + inputSet[i]); | |
throw new AssertionError(); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
long sum = 0; | |
for (int i = 0; i < NUM_REPEAT; i++) { | |
long start, end; | |
Object result; | |
try { | |
start = System.nanoTime(); | |
for (String input: inputSet) { | |
result = method.invoke(null, input); | |
} | |
end = System.nanoTime(); | |
System.out.print((end - start) / 1000 + " "); | |
sum += (end - start) / 1000; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.println("| " + sum / NUM_REPEAT); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment