Created
August 28, 2019 08:01
-
-
Save enfiskutensykkel/554e90ffdbacda34404e14b19e2995eb to your computer and use it in GitHub Desktop.
Java 8 streams
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
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.stream.Collectors; | |
import java.util.stream.LongStream; | |
import java.util.function.Function; | |
import java.util.Arrays; | |
class Stream | |
{ | |
static List<Integer> forLoopA(List<String> strings) | |
{ | |
List<Integer> list = new ArrayList<Integer>(strings.size()); | |
for (String s: strings) { | |
int i = Integer.valueOf(s); | |
if (i % 2 == 0) { | |
list.add(i); | |
} | |
} | |
return list; | |
} | |
static List<Integer> forLoopL(List<String> strings) | |
{ | |
List<Integer> list = new LinkedList<Integer>(); | |
for (String s: strings) { | |
int i = Integer.valueOf(s); | |
if (i % 2 == 0) { | |
list.add(i); | |
} | |
} | |
return list; | |
} | |
static List<Integer> collect(List<String> strings) | |
{ | |
List<Integer> list = strings.stream() | |
.map(Integer::valueOf) | |
.filter(i -> i % 2 == 0) | |
.collect(Collectors.toCollection(ArrayList::new)); | |
return list; | |
} | |
static List<Integer> existing(List<String> strings) | |
{ | |
List<Integer> list = new ArrayList<Integer>(strings.size()); | |
strings.stream() | |
.map(Integer::valueOf) | |
.filter(i -> i % 2 == 0) | |
.forEach(n -> list.add(n)); | |
return list; | |
} | |
static void stats(long[] times, String name) | |
{ | |
double percentiles[] = {99, 97.5, 95, 90, 80, 75, 50, 25, 20, 10, 5, 1}; | |
Arrays.sort(times); | |
System.out.println(); | |
System.out.println(name + " t=" + LongStream.of(times).sum() + ":"); | |
System.out.println("\tmax=" + times[times.length-1]); | |
for (double p: percentiles) { | |
long result = 0; | |
double idx = (p / 100.0) * times.length; | |
if (Math.floor(idx) == idx) { | |
result = (times[(int) idx - 1] + times[(int) idx]) / 2; | |
} else { | |
result = times[(int) Math.floor(idx)]; | |
} | |
System.out.printf("\t.%02.0f=%d", p, result); | |
System.out.println(); | |
} | |
System.out.println("\tmin=" + times[0]); | |
} | |
static long[] benchmark(Function<List<String>, List<Integer>> func, List<String> list, int repeat, String name) | |
{ | |
System.err.print(name + "..."); | |
long times[] = new long[repeat]; | |
long before = System.currentTimeMillis(); | |
for (int i = 0; i < repeat; ++i) { | |
long start = System.currentTimeMillis(); | |
func.apply(list); | |
long stop = System.currentTimeMillis(); | |
times[i] = stop - start; | |
} | |
long after = System.currentTimeMillis(); | |
System.err.println("OK (n=" + list.size() + ", m=" + repeat + ", t=" + (after - before) + ")"); | |
return times; | |
} | |
public static void main(String[] args) | |
{ | |
final int N = 10000000; | |
final int M = 50; | |
List<long[]> times = new ArrayList<long[]>(); | |
System.err.print("Initializing numbers..."); | |
List<String> strings = new ArrayList<String>(N); | |
for (int i = 0; i < N; ++i) { | |
int n = (int) (Math.random() * 1000); | |
strings.add("" + n); | |
} | |
System.err.println("OK"); | |
benchmark(Stream::forLoopA, strings, M, "Warming up"); | |
times.add(benchmark(Stream::forLoopA, strings, M, "forLoopA")); | |
times.add(benchmark(Stream::forLoopL, strings, M, "forLoopL")); | |
times.add(benchmark(Stream::existing, strings, M, "existing")); | |
times.add(benchmark(Stream::collect, strings, M, "collect")); | |
stats(times.get(0), "forLoopA"); | |
stats(times.get(1), "forLoopL"); | |
stats(times.get(2), "existing"); | |
stats(times.get(3), "collect"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment