-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing with benchmark, it is just faster... even with length 10
closes #26
- Loading branch information
1 parent
0e55409
commit b8e8c5b
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
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
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
51 changes: 51 additions & 0 deletions
51
testing/src/jmh/java/xyz/wagyourtail/jvmdg/benchmarks/StreamToListTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package xyz.wagyourtail.jvmdg.benchmarks; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
@State(Scope.Benchmark) | ||
public class StreamToListTest { | ||
|
||
private static final Class<?> REF_PIPELINE; | ||
|
||
static { | ||
try { | ||
REF_PIPELINE = Class.forName("java.util.stream.ReferencePipeline"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void testNewArrayList10(Blackhole bh) { | ||
Stream<Integer> s = IntStream.rangeClosed(0, 10).mapToObj(i -> i + 10); | ||
|
||
List<Integer> l = (List) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(s.toArray()))); | ||
|
||
bh.consume(l); | ||
} | ||
|
||
@Benchmark | ||
public void testCheckClass10(Blackhole bh) { | ||
Stream<Integer> s = IntStream.rangeClosed(0, 10).mapToObj(i -> i + 10); | ||
|
||
List<Integer> l; | ||
if (REF_PIPELINE.isAssignableFrom(s.getClass())) { | ||
l = (List) Collections.unmodifiableList(Arrays.asList(s.toArray())); | ||
} else { | ||
l = (List) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(s.toArray()))); | ||
} | ||
|
||
bh.consume(l); | ||
} | ||
|
||
} |