I would like to know how representative the following benchmark is? Can we infer from it that findFirst
is much slower than findAny
for non-parallel streams? Or is this particular use case too specific?
In addition, I would like to know whether the benchmark has been implemented correctly from a technical point of view.
import java.util.Random;
public class BMTest {
public static void main(String[] args) {
int findFirstFaster = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
int n = 1000 * (i + 1);
long t1 = getTimeFindFirst(n);
long t2 = getTimeFindAny(n);
int faster = t1 < t2 ? 1 : 0;
findFirstFaster += faster;
sb.append(String.format("| %02d | %05d | %010d | %010d | %f | %b |%n", i + 1, n, t1, t2, (double) t1 / (t1 + t2), faster == 1));
}
System.out.println(sb);
System.out.println("findFirstFaster = " + findFirstFaster);
}
public static long getTimeFindFirst(int n) {
Random rand = new Random();
long sum = 0;
long time = System.nanoTime();
for (int i = 0; i < n; i++) {
sum += rand.ints(10_000).filter(x -> x % 7 == 0).findFirst().orElseThrow();
}
time = System.nanoTime() - time;
System.out.println("sum = " + sum);
return time;
}
public static long getTimeFindAny(int n) {
Random rand = new Random();
long sum = 0;
long time = System.nanoTime();
for (int i = 0; i < n; i++) {
sum += rand.ints(10_000).filter(x -> x % 7 == 0).findAny().orElseThrow();
}
time = System.nanoTime() - time;
System.out.println("sum = " + sum);
return time;
}
}
Example output:
01 | 01000 | 0010918901 | 0002353900 | 0,822652 | false |
02 | 02000 | 0001526700 | 0001113200 | 0,578317 | false |
03 | 03000 | 0001618900 | 0001715000 | 0,485587 | true |
04 | 04000 | 0002009100 | 0001740900 | 0,535760 | false |
05 | 05000 | 0002041899 | 0001900000 | 0,517999 | false |
06 | 06000 | 0002077800 | 0002103601 | 0,496915 | true |
07 | 07000 | 0002630900 | 0002274900 | 0,536284 | false |
08 | 08000 | 0002108900 | 0002100399 | 0,501010 | false |
09 | 09000 | 0002266100 | 0002248400 | 0,501960 | false |
10 | 10000 | 0004679400 | 0002199700 | 0,680234 | false |
rand.ints(10_000)
. Pass same set of ints to both tests (calc upfront or use same random seed). \$\endgroup\$