Skip to content

Commit 4f96cfd

Browse files
Semen PopugaevSemen Popugaev
authored andcommitted
first commit
0 parents  commit 4f96cfd

15 files changed

+1056
-0
lines changed

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff:
5+
.idea/workspace.xml
6+
.idea/tasks.xml
7+
.idea/dictionaries
8+
.idea/vcs.xml
9+
.idea/jsLibraryMappings.xml
10+
11+
# Sensitive or high-churn files:
12+
.idea/dataSources.ids
13+
.idea/dataSources.xml
14+
.idea/dataSources.local.xml
15+
.idea/sqlDataSources.xml
16+
.idea/dynamic.xml
17+
.idea/uiDesigner.xml
18+
.idea/
19+
*.iml
20+
21+
# Gradle:
22+
.idea/gradle.xml
23+
.idea/libraries
24+
25+
# Mongo Explorer plugin:
26+
.idea/mongoSettings.xml
27+
28+
## File-based project format:
29+
*.iws
30+
31+
## Plugin-specific files:
32+
33+
# IntelliJ
34+
/out/
35+
36+
# mpeltonen/sbt-idea plugin
37+
.idea_modules/
38+
39+
# JIRA plugin
40+
atlassian-ide-plugin.xml
41+
42+
# Crashlytics plugin (for Android Studio and IntelliJ)
43+
com_crashlytics_export_strings.xml
44+
crashlytics.properties
45+
crashlytics-build.properties
46+
fabric.properties
47+
48+
*.class
49+
*.log
50+
51+
# sbt specific
52+
.cache
53+
.history
54+
.lib/
55+
dist/*
56+
target/
57+
lib_managed/
58+
src_managed/
59+
project/boot/
60+
project/plugins/project/
61+
62+
# Scala-IDE specific
63+
.scala_dependencies
64+
.worksheet

pom.xml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>spliterators</groupId>
6+
<artifactId>spliterators</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.openjdk.jmh</groupId>
13+
<artifactId>jmh-core</artifactId>
14+
<version>${jmh.version}</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.openjdk.jmh</groupId>
18+
<artifactId>jmh-generator-annprocess</artifactId>
19+
<version>${jmh.version}</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-lang3</artifactId>
26+
<version>3.4</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.12</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
37+
<properties>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
<jmh.version>1.12</jmh.version>
40+
<javac.target>1.8</javac.target>
41+
<uberjar.name>benchmarks</uberjar.name>
42+
</properties>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<version>3.1</version>
50+
<configuration>
51+
<compilerVersion>${javac.target}</compilerVersion>
52+
<source>${javac.target}</source>
53+
<target>${javac.target}</target>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-shade-plugin</artifactId>
59+
<version>2.2</version>
60+
<executions>
61+
<execution>
62+
<phase>package</phase>
63+
<goals>
64+
<goal>shade</goal>
65+
</goals>
66+
<configuration>
67+
<finalName>${uberjar.name}</finalName>
68+
<transformers>
69+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
70+
<mainClass>org.openjdk.jmh.Main</mainClass>
71+
</transformer>
72+
</transformers>
73+
<filters>
74+
<filter>
75+
<!--
76+
Shading signed JARs will fail without this.
77+
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
78+
-->
79+
<artifact>*:*</artifact>
80+
<excludes>
81+
<exclude>META-INF/*.SF</exclude>
82+
<exclude>META-INF/*.DSA</exclude>
83+
<exclude>META-INF/*.RSA</exclude>
84+
</excludes>
85+
</filter>
86+
</filters>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
</plugins>
92+
<pluginManagement>
93+
<plugins>
94+
<plugin>
95+
<artifactId>maven-clean-plugin</artifactId>
96+
<version>2.5</version>
97+
</plugin>
98+
<plugin>
99+
<artifactId>maven-deploy-plugin</artifactId>
100+
<version>2.8.1</version>
101+
</plugin>
102+
<plugin>
103+
<artifactId>maven-install-plugin</artifactId>
104+
<version>2.5.1</version>
105+
</plugin>
106+
<plugin>
107+
<artifactId>maven-jar-plugin</artifactId>
108+
<version>2.4</version>
109+
</plugin>
110+
<plugin>
111+
<artifactId>maven-javadoc-plugin</artifactId>
112+
<version>2.9.1</version>
113+
</plugin>
114+
<plugin>
115+
<artifactId>maven-resources-plugin</artifactId>
116+
<version>2.6</version>
117+
</plugin>
118+
<plugin>
119+
<artifactId>maven-site-plugin</artifactId>
120+
<version>3.3</version>
121+
</plugin>
122+
<plugin>
123+
<artifactId>maven-source-plugin</artifactId>
124+
<version>2.2.1</version>
125+
</plugin>
126+
<plugin>
127+
<artifactId>maven-surefire-plugin</artifactId>
128+
<version>2.17</version>
129+
</plugin>
130+
</plugins>
131+
</pluginManagement>
132+
</build>
133+
134+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package spliterators.part1.example;
2+
3+
import java.util.Spliterator;
4+
import java.util.Spliterators;
5+
import java.util.function.IntConsumer;
6+
7+
public class ArrayExample {
8+
public static class IntArraySpliterator extends Spliterators.AbstractIntSpliterator {
9+
10+
private final int[] array;
11+
private int startInclusive;
12+
private final int endExclusive;
13+
14+
public IntArraySpliterator(int[] array) {
15+
this(array, 0, array.length);
16+
}
17+
18+
private IntArraySpliterator(int[] array, int startInclusive, int endExclusive) {
19+
super(endExclusive - startInclusive,
20+
Spliterator.IMMUTABLE
21+
| Spliterator.ORDERED
22+
| Spliterator.SIZED
23+
| Spliterator.SUBSIZED
24+
| Spliterator.NONNULL);
25+
this.array = array;
26+
this.startInclusive = startInclusive;
27+
this.endExclusive = endExclusive;
28+
}
29+
30+
@Override
31+
public boolean tryAdvance(IntConsumer action) {
32+
if (startInclusive < endExclusive) {
33+
action.accept(array[startInclusive]);
34+
startInclusive += 1;
35+
return true;
36+
} else {
37+
return false;
38+
}
39+
}
40+
41+
@Override
42+
public long estimateSize() {
43+
return endExclusive - startInclusive;
44+
}
45+
46+
47+
@Override
48+
public OfInt trySplit() {
49+
int length = endExclusive - startInclusive;
50+
if (length <= 1) {
51+
return null;
52+
}
53+
54+
int middle = startInclusive + length/2;
55+
56+
final IntArraySpliterator newSpliterator = new IntArraySpliterator(array, startInclusive, middle);
57+
58+
startInclusive = middle;
59+
60+
return newSpliterator;
61+
}
62+
}
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package spliterators.part1.example;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
import java.util.Arrays;
6+
import java.util.concurrent.ThreadLocalRandom;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.stream.StreamSupport;
9+
10+
@Fork(1)
11+
@BenchmarkMode(Mode.AverageTime)
12+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
13+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
14+
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
15+
@State(Scope.Thread)
16+
public class ArrayIntBenchmark {
17+
18+
@Param({"100000"})
19+
public int length;
20+
21+
public int[] array;
22+
23+
@Setup
24+
public void setup() {
25+
array = new int[length];
26+
27+
for (int i = 0; i < array.length; i++) {
28+
array[i] = ThreadLocalRandom.current().nextInt();
29+
}
30+
}
31+
32+
33+
@Benchmark
34+
public long baiseline_seq() {
35+
return Arrays.stream(array)
36+
.sequential()
37+
.asLongStream()
38+
.sum();
39+
}
40+
41+
@Benchmark
42+
public long baiseline_par() {
43+
return Arrays.stream(array)
44+
.parallel()
45+
.asLongStream()
46+
.sum();
47+
}
48+
49+
@Benchmark
50+
public long test_seq() {
51+
final boolean parallel = false;
52+
return StreamSupport.intStream(new ArrayExample.IntArraySpliterator(array), parallel)
53+
.asLongStream()
54+
.sum();
55+
}
56+
57+
@Benchmark
58+
public long test_par() {
59+
final boolean parallel = true;
60+
return StreamSupport.intStream(new ArrayExample.IntArraySpliterator(array), parallel)
61+
.asLongStream()
62+
.sum();
63+
}
64+
}

0 commit comments

Comments
 (0)