First commit
This commit is contained in:
commit
61700e7b59
5 files changed
+181
No files matched your search
Generated
+8
@@ -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
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+14
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>stream-examples</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>22</maven.compiler.source>
|
||||||
|
<maven.compiler.target>22</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>1.37</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>1.37</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -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