They are certainly not perfects, but I've tried some ways to optimize the test, here the result (I have had numbers to keep trace of good responses) , and I use a StringBuilder to avoid initialization of output IO:
package exercices;
import java.util.Hashtable;
import org.memneuroo.outils.communs.utilitaires.EnvPrm;
public class FizzBuzz {
// time for cum with nbIter=30 > 300; 30 ~= 3000
static final int nbIter = 30;
static final String sep = "_";
static long ifNested() {
final StringBuilder sb = new StringBuilder();
final long t = System.nanoTime();
for (int i = 0; i < nbIter; i++) {
sb.append(//
i % 15 == 0 //
? "FizzBuzz" //
: (i % 3 == 0 //
? "Fizz"//
: (i % 5 == 0//
? "Buzz" //
: i)));// sb.append(sep);
}
final long totT = System.nanoTime() - t;
System.out.format("ifNested\t%20d\n", totT);
// sb.append(EnvPrm.NEWLINE); System.out.println(sb.toString());
return totT;
}
static long stringPlus() {
final StringBuilder sb = new StringBuilder();
final long t = System.nanoTime();
for (int i = 0; i < nbIter; i++) {
String x = "";
x += (i % 3 == 0) ? "Fizz" : "";
x += (i % 5 == 0) ? "Buzz" : "";
if (x.isEmpty()) { // MODIF
x += Integer.toString(i);
}
sb.append(x);// sb.append(sep);
}
final long totT = System.nanoTime() - t;
System.out.format("stringPlus\t%20d\n", totT);
// sb.append(EnvPrm.NEWLINE); System.out.println(sb.toString());
return totT;
}
static long withIf() {
final StringBuilder sb = new StringBuilder();
final long t = System.nanoTime();
for (int i = 0; i < nbIter; i++) {
if (i % 3 == 0) {
sb.append("Fizz");
if (i % 5 == 0) {
sb.append("Buzz");
}
} else if (i % 5 == 0) {
sb.append("Buzz");
} else {
sb.append(i);
}// sb.append(sep);
}
final long totT = System.nanoTime() - t;
System.out.format("withIf\t\t%20d\n", totT);
// sb.append(EnvPrm.NEWLINE);System.out.println(sb.toString());
return totT;
}
static long withArray() {
final String[] lis = {"FizzBuzz", "", "", "Fizz", "", "Buzz", "Fizz",
"", "", "Fizz", "Buzz", "", "Fizz", "", "",};
final StringBuilder sb = new StringBuilder();
final long t = System.nanoTime();
for (int i = 0; i < nbIter; i++) {
final String pos = lis[i % 15];
sb.append(((0 == pos.length()) ? i : pos));// sb.append(sep);
}
final long totT = System.nanoTime() - t;
System.out.format("withArray\t%20d\n", totT);
// sb.append(EnvPrm.NEWLINE); System.out.println(sb.toString());
return totT;
}
static long withTable() {
final Hashtable<Integer, String> ht = new Hashtable<>(8);
ht.put(0, "FizzBuzz");
ht.put(3, "Fizz");
ht.put(5, "Buzz");
ht.put(6, "Fizz");
ht.put(9, "Fizz");
ht.put(10, "Buzz");
ht.put(12, "Buzz");
final StringBuilder sb = new StringBuilder();
final long t = System.nanoTime();
for (int i = 0; i < nbIter; i++) {
final String s = ht.get(i % 15);
// MODIF
// http://www.developpez.net/forums/d1196563-2/java/general-java/if-null-object-if-objet-null/#post6561766
// sb.append((null == s ? i : s));// sb.append(sep);
if (null == s) {
sb.append(i);
} else {
sb.append(s);
}
}
final long totT = System.nanoTime() - t;
System.out.format("withTable\t%20d\n", totT);
// sb.append(EnvPrm.NEWLINE); System.out.println(sb.toString());
return totT;
}
static int recursive(final StringBuilder sb, final int n) {
if (0 == n) {
return 1;
}
if (n % 3 == 0) {
sb.insert(0, "Fizz");
if (n % 5 == 0) {
sb.insert(0, "Buzz");
}
} else if (n % 5 == 0) {
sb.insert(0, "Buzz");
} else {
sb.insert(0, n);
}
return n + recursive(sb, n - 1);
}
static long recursive() {
final StringBuilder sb = new StringBuilder("");
final long t = System.nanoTime();
recursive(sb, nbIter);
final long totT = System.nanoTime() - t;
System.out.format("recursive\t%20d\n", totT);
sb.append(EnvPrm.NEWLINE);
System.out.println(sb.toString());
return totT;
}
/*** @param args */
public static void main(final String[] args) {
long cum = 0L, cum2 = 0L;
for (int i = 0; i < 5; i++) {
System.out.println("------ " + i + " -----");
final long totSb = stringPlus();
final long totIn = ifNested();
final long totWi = withIf();
final long totWa = withArray();
final long totWt = withTable();
final long totRe = recursive();
System.out.format("... stringPlus/withIf :%5d\n", (totSb * 100)
/ totWi);
System.out.format("... ifNested/withIf :%5d\n", (totIn * 100)
/ totWi);
System.out.format("... withArray/withIf :%5d\n", (totWa * 100)
/ totWi);
System.out.format("... withTable/withIf :%5d\n", (totWt * 100)
/ totWi);
System.out.format("... recursive/withIf :%5d\n", (totRe * 100)
/ totWi);
cum += totIn + totSb + totWi + totWa + totWt + totRe;
System.out.println("CUMUL (SECOND) == " + cum / 100000000 + "."
+ cum % 100000000 + "\t , diff: " + (cum - cum2));
cum2 = cum;
}
}
}
And the output :
------ 0 -----
stringPlus 529397
ifNested 643657
withIf 27657
withArray 43581
withTable 40788
recursive 87441
12Fizz4BuzzFizz78FizzBuzz11Fizz1314BuzzFizz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829BuzzFizz
... stringPlus/withIf : 1914
... ifNested/withIf : 2327
... withArray/withIf : 157
... withTable/withIf : 147
... recursive/withIf : 316
CUMUL (SECOND) == 0.1372521 , diff: 1372521
------ 1 -----
stringPlus 345295
ifNested 88280
withIf 88279
withArray 88838
withTable 101689
recursive 93308
12Fizz4BuzzFizz78FizzBuzz11Fizz1314BuzzFizz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829BuzzFizz
... stringPlus/withIf : 391
... ifNested/withIf : 100
... withArray/withIf : 100
... withTable/withIf : 115
... recursive/withIf : 105
CUMUL (SECOND) == 0.2178210 , diff: 805689
------ 2 -----
stringPlus 380216
ifNested 36597
withIf 20953
withArray 60063
withTable 91352
recursive 111467
12Fizz4BuzzFizz78FizzBuzz11Fizz1314BuzzFizz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829BuzzFizz
... stringPlus/withIf : 1814
... ifNested/withIf : 174
... withArray/withIf : 286
... withTable/withIf : 435
... recursive/withIf : 531
CUMUL (SECOND) == 0.2878858 , diff: 700648
------ 3 -----
stringPlus 489168
ifNested 29613
withIf 22070
withArray 27099
withTable 27378
recursive 91911
12Fizz4BuzzFizz78FizzBuzz11Fizz1314BuzzFizz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829BuzzFizz
... stringPlus/withIf : 2216
... ifNested/withIf : 134
... withArray/withIf : 122
... withTable/withIf : 124
... recursive/withIf : 416
CUMUL (SECOND) == 0.3566097 , diff: 687239
------ 4 -----
stringPlus 143035
ifNested 24025
withIf 15924
withArray 23187
withTable 26819
recursive 87162
12Fizz4BuzzFizz78FizzBuzz11Fizz1314BuzzFizz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829BuzzFizz
... stringPlus/withIf : 898
... ifNested/withIf : 150
... withArray/withIf : 145
... withTable/withIf : 168
... recursive/withIf : 547
CUMUL (SECOND) == 0.3886249 , diff: 320152