import java.util.Random;
public class PlusEmptyStringBench {
private static Random r = new Random();
public static void main(String[] args) {
long nanoTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
doWithCheck();
}
System.out.println("With check: " + (System.nanoTime() - nanoTime));
nanoTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
doWithPlus();
}
System.out.println("With plus: " + (System.nanoTime() - nanoTime));
}
private static String checkNotNull(String forCheck) {
if (forCheck == null) {
return "null";
} else {
return forCheck;
}
}
private static void doWithPlus() {
if (r.nextBoolean()) {
String local = null + "";
} else {
String local = String.valueOf(r.nextBoolean()) + "";
}
}
private static void doWithCheck() {
if (r.nextBoolean()) {
String local = checkNotNull(null);
} else {
String local = checkNotNull(String.valueOf(r.nextBoolean()));
}
}
}
* This source code was highlighted with Source Code Highlighter.
The output of this code is:
With check: 297321208
With plus: 922518553
No comments:
Post a Comment