Skip to content

Instantly share code, notes, and snippets.

@kLeZ
Last active March 25, 2022 10:59
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 kLeZ/c1b3b387521747bd732c32a5834146b7 to your computer and use it in GitHub Desktop.
Save kLeZ/c1b3b387521747bd732c32a5834146b7 to your computer and use it in GitHub Desktop.
Il modulo è LENTO!
import java.util.*;
import java.util.stream.Collectors;
class Scratch {
public static final StringBuilder OUT = new StringBuilder();
public static final boolean PRINT_AT_LAST = true;
public static final boolean PRINT_FIZZ_BUZZ = false;
public static final int FIZZ = 3;
public static final int BUZZ = 5;
public static final int FIZZ_BUZZ = 15;
public static final int MIN_FIZZ_BUZZ = 1;
public static final int MAX_FIZZ_BUZZ = 100;
public static final String FIZZ_STRING = "Fizz";
public static final String BUZZ_STRING = "Buzz";
public static final String FIZZ_BUZZ_STRING = "FizzBuzz";
public static final int ITERATIONS = 1_000_000;
public static void main(String[] args) {
System.out.println(version());
final long startProgram = System.nanoTime();
final Map<Runnable, List<Long>> waysTimes = new HashMap<>();
waysTimes.put(Scratch::way1, new ArrayList<>());
waysTimes.put(Scratch::way2, new ArrayList<>());
waysTimes.put(Scratch::way3, new ArrayList<>());
waysTimes.put(() -> Scratch.way4(MIN_FIZZ_BUZZ, MAX_FIZZ_BUZZ), new ArrayList<>());
for (int i = 0; i < ITERATIONS; i++) {
for (Map.Entry<Runnable, List<Long>> entry : waysTimes.entrySet()) {
final long start;
start = System.nanoTime();
entry.getKey()
.run();
entry.getValue()
.add(System.nanoTime() - start);
}
if (!PRINT_FIZZ_BUZZ)
OUT.delete(0, OUT.length());
}
final List<LongSummaryStatistics> statistics = waysTimes.values()
.stream()
.map(l -> l.stream()
.collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, LongSummaryStatistics::combine))
.collect(Collectors.toList());
final double minOfAvgs = statistics.stream()
.mapToDouble(LongSummaryStatistics::getAverage)
.min()
.orElse(0D);
String minOfAvgsName = "Ukn";
for (int k = 0; k < statistics.size(); k++) {
final LongSummaryStatistics statistic = statistics.get(k);
final double average = statistic.getAverage();
if (average == minOfAvgs) {
minOfAvgsName = String.format("Way%d", k + 1);
break;
}
}
println(System.lineSeparator());
for (int k = 0; k < statistics.size(); k++) {
final LongSummaryStatistics statistic = statistics.get(k);
final double average = statistic.getAverage();
printf("Way%d had an avg of %f ns%n", k + 1, average);
}
printf("Min of avgs is %f ns and belongs to %s%n", minOfAvgs, minOfAvgsName);
printf("Program ran for %f ms%n", (System.nanoTime() - startProgram) / 1000_000D);
flush();
}
private static String version() {
final Properties props = System.getProperties();
final String[] values = {
props.getProperty("java.vm.vendor"),
props.getProperty("java.vm.name"),
props.getProperty("java.version"),
props.getProperty("java.vm.version"),
props.getProperty("java.class.version")
};
return String.join(" ", values);
}
private static void way1() {
for (int i = MIN_FIZZ_BUZZ; i <= MAX_FIZZ_BUZZ; i++) {
if (i % FIZZ_BUZZ == 0) {
println(FIZZ_BUZZ_STRING);
} else if (i % BUZZ == 0) {
println(BUZZ_STRING);
} else if (i % FIZZ == 0) {
println(FIZZ_STRING);
} else {
println(i);
}
}
}
private static void way2() {
for (int i = MIN_FIZZ_BUZZ; i <= MAX_FIZZ_BUZZ; i++) {
boolean status = false;
if (i % FIZZ == 0) {
print(FIZZ_STRING);
status = true;
}
if (i % BUZZ == 0) {
print(BUZZ_STRING);
status = true;
}
if (status) {
println();
} else {
println(i);
}
}
}
private static void way3() {
int temp3 = 0, temp5 = 0;
for (int i = MIN_FIZZ_BUZZ; i <= MAX_FIZZ_BUZZ; i++) {
boolean status = false;
temp3++;
temp5++;
if (temp3 == FIZZ) {
print(FIZZ_STRING);
status = true;
temp3 = 0;
}
if (temp5 == BUZZ) {
print(BUZZ_STRING);
status = true;
temp5 = 0;
}
if (status) {
println();
} else {
println(i);
}
}
}
private static void way4(final int start, final int end) {
if (start <= end) {
boolean mod3 = (start % FIZZ) == 0;
boolean mod5 = (start % BUZZ) == 0;
if (mod3 && mod5) {
println(FIZZ_BUZZ_STRING);
} else if (mod5) {
println(BUZZ_STRING);
} else if (mod3) {
println(FIZZ_STRING);
} else {
println(start);
}
way4(start + 1, end);
}
}
private static void flush() {
if (PRINT_AT_LAST)
System.out.println(OUT);
OUT.delete(0, OUT.length());
}
private static void printf(final String format, final Object... args) {
if (PRINT_AT_LAST)
OUT.append(String.format(format, args));
else
System.out.printf(format, args);
}
private static void print(final String s) {
if (PRINT_AT_LAST)
OUT.append(s);
else
System.out.print(s);
}
private static void println(final int i) {
if (PRINT_AT_LAST)
OUT.append(i)
.append(System.lineSeparator());
else
System.out.println(i);
}
private static void println(final String s) {
if (PRINT_AT_LAST)
OUT.append(s)
.append(System.lineSeparator());
else
System.out.println(s);
}
private static void println() {
if (PRINT_AT_LAST)
OUT.append(System.lineSeparator());
else
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment