Created
May 1, 2013 05:19
-
-
Save spullara/5493858 to your computer and use it in GitHub Desktop.
Collector benchmark
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
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