public abstract static class FloatStream.FloatStreamEx extends FloatStream
FloatStream.FloatStreamEx
BaseStream.Splitor
Modifier and Type | Method and Description |
---|---|
S |
carry(C action)
Same as
peek |
void |
close() |
boolean |
isParallel() |
String |
join(CharSequence delimiter) |
S |
parallel() |
S |
parallel(BaseStream.Splitor splitor) |
S |
parallel(Executor executor) |
S |
parallel(int maxThreadNum) |
S |
parallel(int maxThreadNum,
BaseStream.Splitor splitor)
Returns an equivalent stream that is parallel.
|
S |
parallel(int maxThreadNum,
BaseStream.Splitor splitor,
Executor executor) |
S |
parallel(int maxThreadNum,
Executor executor) |
void |
println() |
S |
sequential() |
S |
shuffled()
This method only run sequentially, even in parallel stream and all elements will be loaded to memory. |
Stream<S> |
sliding(int windowSize) |
Stream<PL> |
slidingToList(int windowSize) |
ImmutableList<T> |
toImmutableList() |
ImmutableSet<T> |
toImmutableSet() |
__, allMatch, anyMatch, append, appendIfEmpty, asDoubleStream, average, boxed, collapse, collapse, collect, collect, concat, concat, concat, concat, empty, findAny, findFirst, findFirstOrLast, findLast, flatMap, flatMappToObj, flatMapToDouble, flatMapToInt, flatMapToLong, flatMapToObj, flatten, flatten, flatten, flatten, flattMap, flattMapToObj, forEach, forEachIndexed, generate, iterate, iterate, iterate, iterate, iterator, kthLargest, map, mapToDouble, mapToInt, mapToLong, mapToObj, max, merge, merge, merge, merge, merge, merge, merge, merge, min, noneMatch, of, of, of, of, of, of, of, parallelMerge, parallelMerge, prepend, random, rangeMap, rangeMapToObj, reduce, reduce, repeat, scan, scan, scan, skipUntil, sum, summarize, summarizeAndPercentiles, toFloatList, toMap, toMap, toMap, toMap, toMap, toMap, top, top, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zipWith, zipWith, zipWith, zipWith
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
acceptIfNotEmpty, append, appendIfEmpty, applyIfNotEmpty, count, difference, distinct, dropWhile, dropWhile, filter, filter, first, indexed, intersection, join, last, limit, onClose, onlyOne, peek, percentiles, prepend, removeIf, removeIf, reversed, reverseSorted, rotated, shuffled, skip, skip, sliding, slidingToList, sorted, split, split, splitAt, splitBy, splitToList, splitToList, step, symmetricDifference, takeWhile, toArray, toCollection, toList, toLongMultiset, toLongMultiset, toMultiset, toMultiset, toSet
public S carry(C action)
BaseStream
peek
carry
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
BaseStream.peek(Object)
public Stream<S> sliding(int windowSize)
sliding
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
BaseStream.sliding(int, int)
public Stream<PL> slidingToList(int windowSize)
slidingToList
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
BaseStream.sliding(int, int)
public S shuffled()
BaseStream
shuffled
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public ImmutableList<T> toImmutableList()
toImmutableList
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public ImmutableSet<T> toImmutableSet()
toImmutableSet
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public String join(CharSequence delimiter)
join
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public void println()
println
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public boolean isParallel()
isParallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S sequential()
sequential
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel()
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel(int maxThreadNum)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel(BaseStream.Splitor splitor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
public S parallel(int maxThreadNum, BaseStream.Splitor splitor)
BaseStream
maxThreadNum
and splitor
as the specified ones.
Profiler.run(1, 1, 3, "sequential", () -> Stream.of(list).operation(F)...).printResult();
Profiler.run(1, 1, 3, "parallel", () -> Stream.of(list).parallel().operation(F)...).printResult();
Here is a sample performance test with computer: CPU Intel i7-3520M 4-cores 2.9 GHz, JDK 1.8.0_101, Windows 7:
public void test_perf() {
final String[] strs = new String[10_000];
N.fill(strs, N.uuid());
final int m = 1;
final Function mapper = str -> {
long result = 0;
for (int i = 0; i < m; i++) {
result += sum(str.toCharArray()) + 1;
}
return result;
};
final MutableLong sum = MutableLong.of(0);
for (int i = 0, len = strs.length; i < len; i++) {
sum.add(mapper.apply(strs[i]));
}
final int threadNum = 1, loopNum = 100, roundNum = 3;
Profiler.run(threadNum, loopNum, roundNum, "For Loop", () -> {
long result = 0;
for (int i = 0, len = strs.length; i < len; i++) {
result += mapper.apply(strs[i]);
}
assertEquals(sum.longValue(), result);
}).printResult();
Profiler.run(threadNum, loopNum, roundNum, "JDK Sequential",
() -> assertEquals(sum.longValue(), java.util.stream.Stream.of(strs).map(mapper).mapToLong(e -> e).sum())).printResult();
Profiler.run(threadNum, loopNum, roundNum, "JDK Parallel",
() -> assertEquals(sum.longValue(), java.util.stream.Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum())).printResult();
Profiler.run(threadNum, loopNum, roundNum, "Abcus Sequential",
() -> assertEquals(sum.longValue(), Stream.of(strs).map(mapper).mapToLong(e -> e).sum().longValue())).printResult();
Profiler.run(threadNum, loopNum, roundNum, "Abcus Parallel",
() -> assertEquals(sum.longValue(), Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum().longValue())).printResult();
}
And test result: Unit is milliseconds. N(the number of elements) is 10_000, Q(cost per element of F, the per-element function (usually a lambda), here is mapper
) is calculated by: value of 'For loop' / N(10_000).
m = 1 | m = 10 | m = 50 | m = 100 | m = 500 | m = 1000 | |
---|---|---|---|---|---|---|
Q | 0.00002 | 0.0002 | 0.001 | 0.002 | 0.01 | 0.02 |
For Loop | 0.23 | 2.3 | 11 | 22 | 110 | 219 |
JDK Sequential | 0.28 | 2.3 | 11 | 22 | 114 | 212 |
JDK Parallel | 0.22 | 1.3 | 6 | 12 | 66 | 122 |
Abcus Sequential | 0.3 | 2 | 11 | 22 | 112 | 212 |
Abcus Parallel | 11 | 11 | 11 | 16 | 77 | 128 |
f = (int a, int b) -> a + b;
.
But if we look into the samples in the article and think about it: it just takes less than 1 milliseconds to get the max value in 100k numbers.
There is potential performance issue only if the "get the max value in 100K numbers" call many, many times in your API or single request.
Otherwise, the difference between 0.1 milliseconds to 0.5 milliseconds can be totally ignored.
Usually we meet performance issue only if Q and F is big enough. However, the performance of Lambdas/Streams APIs is closed to for loop when Q and F is big enough.
No matter in which scenario, We don't need and should not concern the performance of Lambdas/Stream APIs.
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
maxThreadNum
- Default value is the number of cpu-cores. Steps/operations will be executed sequentially if maxThreadNum
is 1.splitor
- The target array is split by ranges for multiple threads if splitor is splitor.ARRAY
and target stream composed by array. It looks like:
for (int i = 0; i < maxThreadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
while (cursor < to) {
action.accept(elements[cursor++]);
}
}
}));
}
Otherwise, each thread will get the elements from the target array/iterator in the stream one by one with the target array/iterator synchronized. It looks like:
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
public void run() {
T next = null;
while (true) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
action.accept(next);
}
}
}));
}
Using splitor.ARRAY
only when F (the per-element function (usually a lambda)) is very tiny and the cost of synchronization on the target array/iterator is too big to it.
For the F involving IO or taking 'long' to complete, choose splitor.ITERATOR
. Default value is splitor.ITERATOR
.MergeResult
,
com.landawn.abacus.util.Profiler#run(int, int, int, String, Runnable)
,
Understanding Parallel Stream Performance in Java SE 8,
When to use parallel Streamspublic S parallel(int maxThreadNum, BaseStream.Splitor splitor, Executor executor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
executor
- should be able to execute maxThreadNum
* following up operations
in parallel.public S parallel(int maxThreadNum, Executor executor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
executor
- should be able to execute maxThreadNum
* following up operations
in parallel.public S parallel(Executor executor)
parallel
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
executor
- should be able to execute maxThreadNum
* following up operations
in parallel.public void close()
close
in interface BaseStream<T,A,P,C,PL,OT,IT,ITER,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
close
in interface AutoCloseable
Copyright © 2020. All rights reserved.