Skip to content

Instantly share code, notes, and snippets.

@spullara
Created May 1, 2013 05:19
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 spullara/5493858 to your computer and use it in GitHub Desktop.
Save spullara/5493858 to your computer and use it in GitHub Desktop.
Collector benchmark
package collectbenchmark;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import static java.lang.Integer.parseInt;
import static java.lang.System.nanoTime;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.IntStream.range;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
int objects = parseInt(args[0]);
int iterations = parseInt(args[1]);
int[] foos = range(0, objects).toArray();
System.gc();
System.out.println("Starting benchmark proper " + objects + " objects; " + iterations + " iterations");
time(iterations, () -> {
collectTest(foos);
});
}
public static void time(int iters, Runnable runnable) {
long start = nanoTime();
for (int i = 0; i < iters; i++) {
runnable.run();
}
long elapsed = nanoTime() - start;
System.out.println("Elapsed time: " + elapsed + "ns");
}
public static int makeBar(int x) {
return x * 2;
}
public static Map<Integer, Integer> collectTest(int[] foos) {
return (Map<Integer, Integer>) Arrays.stream(foos).boxed().collect(toMap(identity(), App::makeBar));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment