From 61700e7b591c5981a2cec884396c6b84ae9d5ad6 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sat, 7 Sep 2024 20:48:02 -0400 Subject: [PATCH] First commit --- .idea/.gitignore | 8 ++ .idea/encodings.xml | 7 + .idea/misc.xml | 14 ++ pom.xml | 30 +++++ .../java/com/alldressedprogramming/Main.java | 122 ++++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/alldressedprogramming/Main.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..039a9d1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..640de10 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.example + stream-examples + 1.0-SNAPSHOT + + + 22 + 22 + UTF-8 + + + + + org.openjdk.jmh + jmh-core + 1.37 + + + org.openjdk.jmh + jmh-generator-annprocess + 1.37 + + + + \ No newline at end of file diff --git a/src/main/java/com/alldressedprogramming/Main.java b/src/main/java/com/alldressedprogramming/Main.java new file mode 100644 index 0000000..db4a75b --- /dev/null +++ b/src/main/java/com/alldressedprogramming/Main.java @@ -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 composers) { + } + + public record Album(List 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 createFakeAlbums(int n) { + return LongStream.range(0, n) + .mapToObj(Random::new) + .map(Main::createFakeAlbum) + .toList(); + + } + + List albums; + + @Setup(Level.Invocation) + public void setUp() { + albums = createFakeAlbums(1000); + } + + + @Benchmark + public void classical(Blackhole blackhole) { + List 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 getRecentAlbums(List albums){ + List ret = new ArrayList<>(); + for(Album album: albums){ + if(album.releaseYear() > 2020){ + ret.add(album); + } + } + return ret; + } + + public List getTaylorSwiftSongs(List albums){ + List 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 recentAlbums = getRecentAlbums(albums); + List newTaylorSwift = getTaylorSwiftSongs(recentAlbums); + blackhole.consume(newTaylorSwift); + } + + public Stream getRecentAlbumsAsStream(Stream albums){ + return albums.filter(album -> album.releaseYear() > 2020); + } + + public Stream getTaylorSwiftSongsAsStream(Stream albums){ + return albums + .flatMap(album -> album.songs().stream()) + .filter(song -> song.composers().contains("taylor swift")); + } + + @Benchmark + public void brokenDownWithMethodsWithStreams(Blackhole blackhole) { + Stream recentAlbums = getRecentAlbumsAsStream(albums.stream()); + Stream newTaylorSwift = getTaylorSwiftSongsAsStream(recentAlbums); + blackhole.consume(newTaylorSwift); + } + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } +} \ No newline at end of file