First commit
This commit is contained in:
commit
61700e7b59
5 files changed
+181
No files matched your search
@@ -0,0 +1,122 @@
|
||||
package com.alldressedprogramming;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public class Main {
|
||||
|
||||
public record Song(List<String> composers) {
|
||||
}
|
||||
|
||||
public record Album(List<Song> songs, int releaseYear) {
|
||||
}
|
||||
|
||||
public static Song createFakeSong(Random random){
|
||||
var composers = IntStream.range(1,10)
|
||||
.map(i -> random.nextInt(0, 100))
|
||||
.mapToObj(i -> i == 0 ? "taylor swift" : "not taylor swift #" + i )
|
||||
.toList();
|
||||
return new Song(composers);
|
||||
|
||||
}
|
||||
|
||||
public static Album createFakeAlbum(Random random) {
|
||||
var seed = random.nextLong();
|
||||
var songs = IntStream.range(2, 25)
|
||||
.mapToObj(Random::new)
|
||||
.map(Main::createFakeSong)
|
||||
.toList();
|
||||
return new Album(songs, random.nextInt(1990, 2024));
|
||||
}
|
||||
|
||||
public static List<Album> createFakeAlbums(int n) {
|
||||
return LongStream.range(0, n)
|
||||
.mapToObj(Random::new)
|
||||
.map(Main::createFakeAlbum)
|
||||
.toList();
|
||||
|
||||
}
|
||||
|
||||
List<Album> albums;
|
||||
|
||||
@Setup(Level.Invocation)
|
||||
public void setUp() {
|
||||
albums = createFakeAlbums(1000);
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
public void classical(Blackhole blackhole) {
|
||||
List<Song> newTaylorSwift = new ArrayList<>();
|
||||
for(Album album: albums){
|
||||
if(album.releaseYear() > 2020){
|
||||
for(Song song: album.songs()){
|
||||
if(song.composers().contains("taylor swift")){
|
||||
newTaylorSwift.add(song);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
blackhole.consume(newTaylorSwift);
|
||||
}
|
||||
|
||||
|
||||
public List<Album> getRecentAlbums(List<Album> albums){
|
||||
List<Album> ret = new ArrayList<>();
|
||||
for(Album album: albums){
|
||||
if(album.releaseYear() > 2020){
|
||||
ret.add(album);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<Song> getTaylorSwiftSongs(List<Album> albums){
|
||||
List<Song> ret = new ArrayList<>();
|
||||
for(Album album: albums){
|
||||
for(Song song: album.songs()){
|
||||
if(song.composers().contains("taylor swift")){
|
||||
ret.add(song);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void brokenDownWithMethods(Blackhole blackhole) {
|
||||
List<Album> recentAlbums = getRecentAlbums(albums);
|
||||
List<Song> newTaylorSwift = getTaylorSwiftSongs(recentAlbums);
|
||||
blackhole.consume(newTaylorSwift);
|
||||
}
|
||||
|
||||
public Stream<Album> getRecentAlbumsAsStream(Stream<Album> albums){
|
||||
return albums.filter(album -> album.releaseYear() > 2020);
|
||||
}
|
||||
|
||||
public Stream<Song> getTaylorSwiftSongsAsStream(Stream<Album> albums){
|
||||
return albums
|
||||
.flatMap(album -> album.songs().stream())
|
||||
.filter(song -> song.composers().contains("taylor swift"));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void brokenDownWithMethodsWithStreams(Blackhole blackhole) {
|
||||
Stream<Album> recentAlbums = getRecentAlbumsAsStream(albums.stream());
|
||||
Stream<Song> newTaylorSwift = getTaylorSwiftSongsAsStream(recentAlbums);
|
||||
blackhole.consume(newTaylorSwift);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user