Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.Spliterator;
import java.util.Objects;
import java.util.function.Consumer;
class ConstantSpliterator<T> implements Spliterator<T> {
long remaining;
T value;
public ConstantSpliterator(long remaining, T value) {
this.remaining = remaining;
this.value = value;
}
@Override
public Spliterator<T> trySplit() {
long remaining = this.remaining;
if (remaining >= 2) {
remaining >>= 1;
Spliterator<T> prefix = new ConstantSpliterator<>(remaining, value);
this.remaining -= remaining;
return prefix;
}
return null;
}
@Override
public long estimateSize() {
return remaining;
}
@Override
public int characteristics() {
return SIZED | SUBSIZED | IMMUTABLE;
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
Objects.requireNonNull(action);
if (remaining <= 0)
return false;
action.accept(value);
remaining--;
return true;
}
@Override
public void forEachRemaining(Consumer<? super T> action) {
Objects.requireNonNull(action);
T value = this.value;
for (long r = remaining; r > 0; r--) {
action.accept(value);
}
remaining = 0;
}
}
import java.util.*;
import java.util.stream.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(3)
@State(Scope.Benchmark)
public class CopyTest {
@Param({"1", "100", "10000", "1000000", "100000000"})
private int LIMIT;
public static final long VALUE = 3L;
@Setup
public void setup() {
for(int n=0; n<500; n++) {
StreamSupport.stream(new ConstantSpliterator<>(3, 1L), false).mapToLong(i->i).sum();
StreamSupport.stream(new ConstantSpliterator<>(3, 1L), false).mapToLong(i->i).sum();
IntStream.range(0,1).mapToObj(i->i).mapToLong(i->i).sum();
IntStream.range(0,1).mapToObj(i->i).mapToLong(i->i).sum();
}
}
@Benchmark
public long ncopiesParallel() {
return
Collections.nCopies(LIMIT, VALUE)
.parallelStream()
.mapToLong(i -> i)
.map(i -> i % 73 % 13)
.sum();
}
@Benchmark
public long customParallel() {
return
StreamSupport.stream(new ConstantSpliterator<>(LIMIT, VALUE), true)
.mapToLong(i -> i)
.map(i -> i % 73 % 13)
.sum();
}
@Benchmark
public long ncopies() {
return
Collections.nCopies(LIMIT, VALUE)
.stream()
.mapToLong(i -> i)
.map(i -> i % 73 % 13)
.sum();
}
@Benchmark
public long custom() {
return
StreamSupport.stream(new ConstantSpliterator<>(LIMIT, VALUE), false)
.mapToLong(i -> i)
.map(i -> i % 73 % 13)
.sum();
}
}
#JDK9: build 1.9.0-ea-b72
Benchmark (LIMIT) Mode Cnt Score Error Units
CopyTest.custom 1 avgt 30 0.086 ± 0.001 us/op
CopyTest.custom 100 avgt 30 1.063 ± 0.006 us/op
CopyTest.custom 10000 avgt 30 97.060 ± 0.646 us/op
CopyTest.custom 1000000 avgt 30 9705.588 ± 64.237 us/op
CopyTest.custom 100000000 avgt 30 961011.009 ± 4570.314 us/op
CopyTest.customParallel 1 avgt 30 0.125 ± 0.002 us/op
CopyTest.customParallel 100 avgt 30 11.604 ± 0.179 us/op
CopyTest.customParallel 10000 avgt 30 48.848 ± 2.341 us/op
CopyTest.customParallel 1000000 avgt 30 3278.566 ± 38.823 us/op
CopyTest.customParallel 100000000 avgt 30 323927.476 ± 2792.189 us/op
CopyTest.ncopies 1 avgt 30 0.121 ± 0.002 us/op
CopyTest.ncopies 100 avgt 30 1.365 ± 0.011 us/op
CopyTest.ncopies 10000 avgt 30 123.654 ± 0.384 us/op
CopyTest.ncopies 1000000 avgt 30 12378.074 ± 88.937 us/op
CopyTest.ncopies 100000000 avgt 30 1247185.172 ± 16285.856 us/op
CopyTest.ncopiesParallel 1 avgt 30 0.162 ± 0.004 us/op
CopyTest.ncopiesParallel 100 avgt 30 12.227 ± 0.028 us/op
CopyTest.ncopiesParallel 10000 avgt 30 58.770 ± 0.434 us/op
CopyTest.ncopiesParallel 1000000 avgt 30 4299.138 ± 105.004 us/op
CopyTest.ncopiesParallel 100000000 avgt 30 423491.452 ± 2493.405 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment