added nix
This commit is contained in:
1 parent
61700e7b59
commit
1fab7b30c1
11 files changed
+334
-13
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package com.alldressedprogramming;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
@@ -11,7 +12,12 @@ import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
@State(Scope.Thread)
|
||||
@Fork( 1 )
|
||||
@Warmup( iterations = 2 )
|
||||
@Measurement( iterations = 10 )
|
||||
@BenchmarkMode( Mode.AverageTime )
|
||||
@OutputTimeUnit( TimeUnit.MICROSECONDS )
|
||||
public class Main {
|
||||
|
||||
public record Song(List<String> composers) {
|
||||
@@ -30,7 +36,6 @@ public class Main {
|
||||
}
|
||||
|
||||
public static Album createFakeAlbum(Random random) {
|
||||
var seed = random.nextLong();
|
||||
var songs = IntStream.range(2, 25)
|
||||
.mapToObj(Random::new)
|
||||
.map(Main::createFakeSong)
|
||||
@@ -43,16 +48,9 @@ public class Main {
|
||||
.mapToObj(Random::new)
|
||||
.map(Main::createFakeAlbum)
|
||||
.toList();
|
||||
|
||||
}
|
||||
|
||||
List<Album> albums;
|
||||
|
||||
@Setup(Level.Invocation)
|
||||
public void setUp() {
|
||||
albums = createFakeAlbums(1000);
|
||||
}
|
||||
|
||||
final List<Album> albums = createFakeAlbums(10_000);
|
||||
|
||||
@Benchmark
|
||||
public void classical(Blackhole blackhole) {
|
||||
@@ -92,6 +90,15 @@ public class Main {
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void brokenDownWithMethodsWith10Duplicates(Blackhole blackhole) {
|
||||
for(int i = 0; i < 10; i++){
|
||||
List<Album> recentAlbums = getRecentAlbums(albums);
|
||||
List<Song> newTaylorSwift = getTaylorSwiftSongs(recentAlbums);
|
||||
blackhole.consume(newTaylorSwift);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void brokenDownWithMethods(Blackhole blackhole) {
|
||||
List<Album> recentAlbums = getRecentAlbums(albums);
|
||||
@@ -113,10 +120,17 @@ public class Main {
|
||||
public void brokenDownWithMethodsWithStreams(Blackhole blackhole) {
|
||||
Stream<Album> recentAlbums = getRecentAlbumsAsStream(albums.stream());
|
||||
Stream<Song> newTaylorSwift = getTaylorSwiftSongsAsStream(recentAlbums);
|
||||
blackhole.consume(newTaylorSwift);
|
||||
blackhole.consume(newTaylorSwift.toList());
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
// var main = new Main();
|
||||
// var albums = Main.createFakeAlbums(100);
|
||||
// List<Album> recentAlbums = main.getRecentAlbums(albums);
|
||||
// List<Song> newTaylorSwift = main.getTaylorSwiftSongs(recentAlbums);
|
||||
// System.out.println(newTaylorSwift.size());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package com.alldressedprogramming;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Fork( 1 )
|
||||
@Warmup( iterations = 2 )
|
||||
@Measurement( iterations = 10 )
|
||||
@BenchmarkMode( Mode.AverageTime )
|
||||
@OutputTimeUnit( TimeUnit.MILLISECONDS )
|
||||
public class StreamBenchmarks
|
||||
{
|
||||
|
||||
private static final int LIMIT = 10_000;
|
||||
|
||||
enum Foo
|
||||
{
|
||||
ONE( 1 ), TWO( 2 ), THREE( 3 ), FOUR( 4 ), FIVE( 5 ), SIX( 6 ), SEVEN( 7 ), EIGHT( 8 );
|
||||
|
||||
private final int value;
|
||||
|
||||
Foo( int value )
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void iterationWithForLoop( Blackhole blackhole )
|
||||
{
|
||||
for ( int i = 0; i < LIMIT; i++ )
|
||||
{
|
||||
var ret = new ArrayList<Integer>();
|
||||
for ( var foo : Foo.values() )
|
||||
{
|
||||
if ( foo.getValue() % 2 == 0 )
|
||||
{
|
||||
ret.add( foo.getValue() );
|
||||
}
|
||||
}
|
||||
blackhole.consume( ret );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void iterationWithStream( Blackhole blackhole )
|
||||
{
|
||||
for ( int i = 0; i < LIMIT; i++ )
|
||||
{
|
||||
var ret = Stream.of( Foo.values() )
|
||||
.map( Foo::getValue )
|
||||
.filter( x -> x % 2 == 0 )
|
||||
.toList();
|
||||
blackhole.consume( ret );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void findFirstWithForLoop( Blackhole blackhole )
|
||||
{
|
||||
for ( int i = 0; i < LIMIT; i++ )
|
||||
{
|
||||
int ret = -1;
|
||||
for ( var foo : Foo.values() )
|
||||
{
|
||||
if ( foo.getValue() == 6 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
blackhole.consume( ret );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void findFirstWithStream( Blackhole blackhole )
|
||||
{
|
||||
for ( int i = 0; i < LIMIT; i++ )
|
||||
{
|
||||
var ret = Stream.of( Foo.values() )
|
||||
.map( Foo::getValue )
|
||||
.filter( x -> x == 6 )
|
||||
.findFirst()
|
||||
.orElse( -1 );
|
||||
blackhole.consume( ret );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main( String[] args )
|
||||
throws Exception
|
||||
{
|
||||
org.openjdk.jmh.Main.main( args );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user