Initial
This commit is contained in:
commit
726ffb866e
42
pom.xml
Normal file
42
pom.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<?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>com.annimon</groupId>
|
||||
<artifactId>PerformanceBenchmark</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.10.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>1.10.1</version>
|
||||
</dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>stream</artifactId>
|
||||
<version>1.0.9</version>
|
||||
</dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.annimon</groupId>
|
||||
<artifactId>stream</artifactId>
|
||||
<version>1.1.4</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>stream</artifactId>
|
||||
<version>1.1.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<name>PerformanceBenchmark</name>
|
||||
</project>
|
294
src/main/java/com/annimon/GroupCollector.java
Normal file
294
src/main/java/com/annimon/GroupCollector.java
Normal file
@ -0,0 +1,294 @@
|
||||
package com.annimon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class GroupCollector<T, K, R> {
|
||||
|
||||
private final Function<? super T, ? extends K> groupClassifier;
|
||||
private final List<Collector<T, ?, ?>> collectors;
|
||||
private Optional<BiFunction<K, List<?>, R>> finalFinisher;
|
||||
|
||||
public static <T, K, R> GroupCollector<T, K, R> groupBy(Function<? super T, ? extends K> classifier) {
|
||||
return new GroupCollector<>(classifier);
|
||||
}
|
||||
|
||||
public GroupCollector(Function<? super T, ? extends K> groupClassifier) {
|
||||
this.groupClassifier = groupClassifier;
|
||||
collectors = new ArrayList<>();
|
||||
finalFinisher = Optional.empty();
|
||||
}
|
||||
|
||||
public GroupCollector<T, K, R> add(Collector<T, ?, ?> e) {
|
||||
collectors.add(e);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GroupCollector<T, K, R> addAll(Collection<? extends Collector<T, ?, ?>> c) {
|
||||
collectors.addAll(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GroupCollector<T, K, R> finishWith(BiFunction<K, List<?>, R> finalFinisher) {
|
||||
this.finalFinisher = Optional.ofNullable(finalFinisher);
|
||||
return this;
|
||||
}
|
||||
|
||||
public <U> Collector<T, ?, U> collect(Collector<R, ?, U> finalCollector) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Collector<T, List<Object>, List<?>> chainCollector = Collector.of(() -> collectors.stream()
|
||||
.map(Collector::supplier)
|
||||
.map(Supplier::get)
|
||||
.collect(Collectors.toList()),
|
||||
(list, e) -> {
|
||||
IntStream.range(0, collectors.size()).forEach(
|
||||
i -> ((BiConsumer<Object, T>) collectors.get(i).accumulator()).accept(list.get(i), e)
|
||||
);
|
||||
},
|
||||
(l1, l2) -> {
|
||||
IntStream.range(0, collectors.size()).forEach(
|
||||
i -> l1.set(i, ((BinaryOperator<Object>) collectors.get(i).combiner()).apply(l1.get(i), l2.get(i)))
|
||||
);
|
||||
return l1;
|
||||
},
|
||||
list -> {
|
||||
IntStream.range(0, collectors.size()).forEach(
|
||||
i -> list.set(i, ((Function<Object, Object>) collectors.get(i).finisher()).apply(list.get(i)))
|
||||
);
|
||||
return list;
|
||||
}
|
||||
);
|
||||
|
||||
final Collector<T, ?, ? extends Map<? extends K, List<?>>> groupingBy = Collectors
|
||||
.groupingBy(groupClassifier, chainCollector);
|
||||
|
||||
return Collectors.collectingAndThen(groupingBy, map -> map.entrySet().stream()
|
||||
.map(e -> finalFinisher.get().apply(e.getKey(), e.getValue()))
|
||||
.collect(finalCollector)
|
||||
);
|
||||
}
|
||||
}
|
||||
//
|
||||
//package com.annimon.stream;
|
||||
//
|
||||
//import com.annimon.stream.function.BiConsumer;
|
||||
//import com.annimon.stream.function.BiFunction;
|
||||
//import com.annimon.stream.function.Consumer;
|
||||
//import com.annimon.stream.function.Function;
|
||||
//import com.annimon.stream.function.Supplier;
|
||||
//import com.annimon.stream.function.ToDoubleFunction;
|
||||
//import com.annimon.stream.function.ToIntFunction;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.Arrays;
|
||||
//import java.util.Collection;
|
||||
//import java.util.List;
|
||||
//import java.util.Map;
|
||||
//
|
||||
//public class GroupCollector<T, K, R> {
|
||||
//
|
||||
// private final Function<? super T, ? extends K> groupClassifier;
|
||||
// private final List<Collector<T, ?, ?>> collectors;
|
||||
// private Optional<BiFunction<K, List<?>, R>> finalFinisher;
|
||||
//
|
||||
// public static <T, K, R> GroupCollector<T, K, R> groupBy(Function<? super T, ? extends K> classifier) {
|
||||
// return new GroupCollector<T, K, R>(classifier);
|
||||
// }
|
||||
//
|
||||
// public GroupCollector(Function<? super T, ? extends K> groupClassifier) {
|
||||
// this.groupClassifier = groupClassifier;
|
||||
// collectors = new ArrayList<Collector<T, ?, ?>>();
|
||||
// finalFinisher = Optional.empty();
|
||||
// }
|
||||
//
|
||||
// public GroupCollector<T, K, R> add(Collector<T, ?, ?> e) {
|
||||
// collectors.add(e);
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// public GroupCollector<T, K, R> addAll(Collection<? extends Collector<T, ?, ?>> c) {
|
||||
// collectors.addAll(c);
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// public GroupCollector<T, K, R> finishWith(BiFunction<K, List<?>, R> finalFinisher) {
|
||||
// this.finalFinisher = Optional.ofNullable(finalFinisher);
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// public <U> Collector<T, ?, U> collect(final Collector<R, ?, U> finalCollector) {
|
||||
// @SuppressWarnings("unchecked")
|
||||
// final Collector<T, List<Object>, List<?>> chainCollector = new Collector<T, List<Object>, List<?>>() {
|
||||
//
|
||||
// @Override
|
||||
// public Supplier<List<Object>> supplier() {
|
||||
// return new Supplier<List<Object>>() {
|
||||
// @Override
|
||||
// public List<Object> get() {
|
||||
// return Stream.of(collectors)
|
||||
// .map(new Function<Collector<T, ?, ?>, Object>() {
|
||||
// @Override
|
||||
// public Object apply(Collector<T, ?, ?> collector) {
|
||||
// return collector.supplier().get();
|
||||
// }
|
||||
// })
|
||||
// .collect(Collectors.toList());
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public BiConsumer<List<Object>, T> accumulator() {
|
||||
// return new BiConsumer<List<Object>, T>() {
|
||||
// @Override
|
||||
// public void accept(final List<Object> list, final T value) {
|
||||
// Stream.of(collectors)
|
||||
// .indexed()
|
||||
// .forEach(new Consumer<IntPair<Collector<T, ?, ?>>>() {
|
||||
// @Override
|
||||
// public void accept(IntPair<Collector<T, ?, ?>> p) {
|
||||
// ((BiConsumer<Object, T>) collectors.get(p.getFirst()).accumulator())
|
||||
// .accept(list.get(p.getFirst()), value);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Function<List<Object>, List<?>> finisher() {
|
||||
// return new Function<List<Object>, List<?>>() {
|
||||
// @Override
|
||||
// public List<?> apply(final List<Object> list) {
|
||||
// Stream.of(collectors)
|
||||
// .indexed()
|
||||
// .forEach(new Consumer<IntPair<Collector<T, ?, ?>>>() {
|
||||
// @Override
|
||||
// public void accept(IntPair<Collector<T, ?, ?>> p) {
|
||||
// final int i = p.getFirst();
|
||||
// list.set(i, ((Function<Object, Object>) collectors.get(i).finisher())
|
||||
// .apply(list.get(i)));
|
||||
// }
|
||||
// });
|
||||
// return list;
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// final Collector<T, ?, ? extends Map<? extends K, List<?>>> groupingBy = Collectors
|
||||
// .groupingBy(groupClassifier, chainCollector);
|
||||
//
|
||||
// return Collectors.collectingAndThen(groupingBy, new Function<Map<? extends K, List<?>>, U>() {
|
||||
// @Override
|
||||
// public U apply(Map<? extends K, List<?>> map) {
|
||||
// return Stream.of(map.entrySet())
|
||||
// .map(new Function<Map.Entry<? extends K, List<?>>, R>() {
|
||||
// @Override
|
||||
// public R apply(Map.Entry<? extends K, List<?>> e) {
|
||||
// return finalFinisher.get().apply(e.getKey(), e.getValue());
|
||||
// }
|
||||
// })
|
||||
// .collect(finalCollector);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// static class Data {
|
||||
// String key;
|
||||
// int value, count;
|
||||
// double average;
|
||||
//
|
||||
// Data(String key, int value, int count) {
|
||||
// this(key, value, count, 0d);
|
||||
// }
|
||||
//
|
||||
// Data(String key, int value, int count, double average) {
|
||||
// this.key = key;
|
||||
// this.value = value;
|
||||
// this.count = count;
|
||||
// this.average = average;
|
||||
// }
|
||||
//
|
||||
// public Data setKey(String key) {
|
||||
// this.key = key;
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// public String getKey() {
|
||||
// return key;
|
||||
// }
|
||||
//
|
||||
// public int getValue() {
|
||||
// return value;
|
||||
// }
|
||||
//
|
||||
// public int getCount() {
|
||||
// return count;
|
||||
// }
|
||||
//
|
||||
// public double getAverage() {
|
||||
// return average;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String toString() {
|
||||
// return "Data{" + "key=" + key + ", value=" + value + ", count=" + count + ", average=" + average + '}';
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// List<Data> data = Arrays.asList(
|
||||
// new Data("3228-hc43", 100, 400, 21.0),
|
||||
// new Data("6759-4330", 123, 67, 10.4),
|
||||
// new Data("a3f5-ac2g", 363, 13, 13),
|
||||
// new Data("3228-hc43", 404, 819, 3.14),
|
||||
// new Data("a3f5-ac2g", 3228, 66, 18),
|
||||
// new Data("3228-hc43", 119, 42, -18.5)
|
||||
// );
|
||||
// List<Data> grouped = Stream.of(data)
|
||||
// .collect(GroupCollector.<Data, String, Data>groupBy(new Function<Data, String>() {
|
||||
// @Override
|
||||
// public String apply(Data t) {
|
||||
// return t.getKey();
|
||||
// }
|
||||
// })
|
||||
// .add(Collectors.summingInt(new ToIntFunction<Data>() {
|
||||
// @Override
|
||||
// public int applyAsInt(Data t) {
|
||||
// return t.getValue();
|
||||
// }
|
||||
// }))
|
||||
// .add(Collectors.summingInt(new ToIntFunction<Data>() {
|
||||
// @Override
|
||||
// public int applyAsInt(Data t) {
|
||||
// return t.getCount();
|
||||
// }
|
||||
// }))
|
||||
// .add(Collectors.averagingDouble(new ToDoubleFunction<Data>() {
|
||||
// @Override
|
||||
// public double applyAsDouble(Data t) {
|
||||
// return t.getAverage();
|
||||
// }
|
||||
// }))
|
||||
// .finishWith(new BiFunction<String, List<?>, Data>() {
|
||||
// @Override
|
||||
// public Data apply(String k, List<?> v) {
|
||||
// return new Data(k, (Integer) v.get(0), (Integer) v.get(1), (Double) v.get(2));
|
||||
// }
|
||||
// })
|
||||
// .collect(Collectors.<Data>toList()));
|
||||
// System.out.println(grouped);
|
||||
// }
|
||||
//}
|
||||
|
66
src/main/java/com/annimon/Issue88.java
Normal file
66
src/main/java/com/annimon/Issue88.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.annimon;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public final class Issue88 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final List<AnswerType> ansTypes = Arrays.asList(AnswerType.values());
|
||||
|
||||
Stream<ChatQuestion> stream1 = Stream.range(0, 4)
|
||||
.flatMap(i -> Stream.of(new ChatQuestion()));
|
||||
Stream<ChatAnswer> stream2 = Stream.zip(
|
||||
Stream.range(0, 4)
|
||||
.flatMap(i -> Stream.of(new ChatAnswer())),
|
||||
Stream.of(ansTypes),
|
||||
(value1, value2) -> {
|
||||
value1.setAnswerType(value2);
|
||||
return value1;
|
||||
});
|
||||
|
||||
final List<ChatQuestion> chatQuestions = new ArrayList<>();
|
||||
Stream.zip(stream1, stream2, (value1, value2) -> {
|
||||
value1.getAnswers().add(value2);
|
||||
return value1;
|
||||
}).forEach(chatQuestion -> chatQuestions.add(chatQuestion));
|
||||
|
||||
Stream.of(chatQuestions).forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static class ChatQuestion {
|
||||
|
||||
private final List<ChatAnswer> answers;
|
||||
|
||||
public ChatQuestion() {
|
||||
this.answers = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<ChatAnswer> getAnswers() {
|
||||
return answers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChatQuestion: " + answers.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ChatAnswer {
|
||||
|
||||
private AnswerType answerType;
|
||||
|
||||
public void setAnswerType(AnswerType type) {
|
||||
this.answerType = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChatAnswer{" + "answerType=" + answerType + '}';
|
||||
}
|
||||
}
|
||||
|
||||
private enum AnswerType { TYPE1, TYPE2, TYPE3, TYPE4 };
|
||||
}
|
144
src/main/java/com/annimon/Merger.java
Normal file
144
src/main/java/com/annimon/Merger.java
Normal file
@ -0,0 +1,144 @@
|
||||
package com.annimon;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public final class Merger {
|
||||
|
||||
static class Data {
|
||||
String key;
|
||||
int value, count;
|
||||
double average;
|
||||
|
||||
Data(String key, int value, int count) {
|
||||
this(key, value, count, 0d);
|
||||
}
|
||||
|
||||
Data(String key, int value, int count, double average) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.count = count;
|
||||
this.average = average;
|
||||
}
|
||||
|
||||
public Data setKey(String key) {
|
||||
this.key = key;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public double getAverage() {
|
||||
return average;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Data{" + "key=" + key + ", value=" + value + ", count=" + count + ", average=" + average + '}';
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Data> data = Arrays.asList(
|
||||
new Data("3228-hc43", 100, 400, 21.0),
|
||||
new Data("6759-4330", 123, 67, 10.4),
|
||||
new Data("a3f5-ac2g", 363, 13, 13),
|
||||
new Data("3228-hc43", 404, 819, 3.14),
|
||||
new Data("a3f5-ac2g", 3228, 66, 18),
|
||||
new Data("3228-hc43", 119, 42, -18.5)
|
||||
);
|
||||
|
||||
System.out.println("1");
|
||||
data.stream()
|
||||
.collect(Collectors.groupingBy(Data::getKey))
|
||||
.entrySet().stream()
|
||||
.map(entry -> entry.getValue().stream()
|
||||
.reduce(new Data(entry.getKey(), 0, 0), (t, u) -> {
|
||||
t.value += u.value;
|
||||
t.count += u.count;
|
||||
return t;
|
||||
}))
|
||||
.forEach(System.out::println);
|
||||
|
||||
System.out.println("2");
|
||||
data.stream()
|
||||
.collect(Collectors.collectingAndThen(
|
||||
Collectors.groupingBy(Data::getKey),
|
||||
grouped -> grouped.entrySet().stream()
|
||||
.map(entry -> entry.getValue().stream()
|
||||
.reduce(new Data(entry.getKey(), 0, 0), (t, u) -> {
|
||||
t.value += u.value;
|
||||
t.count += u.count;
|
||||
return t;
|
||||
}
|
||||
)
|
||||
)))
|
||||
.forEach(System.out::println);
|
||||
|
||||
System.out.println("3");
|
||||
data.stream()
|
||||
.collect(Collectors.groupingBy(Data::getKey, chain(
|
||||
v -> new Data("", (Integer) v.get(0), (Integer) v.get(1), (Double) v.get(2)),
|
||||
Collectors.summingInt(Data::getValue),
|
||||
Collectors.summingInt(Data::getCount),
|
||||
Collectors.averagingDouble(Data::getAverage)
|
||||
)))
|
||||
.entrySet().stream()
|
||||
.map(entry -> entry.getValue().setKey(entry.getKey()))
|
||||
.forEach(System.out::println);
|
||||
|
||||
System.out.println("4");
|
||||
data.stream()
|
||||
.collect(GroupCollector.groupBy(Data::getKey)
|
||||
.add(Collectors.summingInt(Data::getValue))
|
||||
.add(Collectors.summingInt(Data::getCount))
|
||||
.add(Collectors.averagingDouble(Data::getAverage))
|
||||
.finishWith((k, v) -> new Data(k, (Integer) v.get(0), (Integer) v.get(1), (Double) v.get(2)))
|
||||
.collect(Collectors.toList()))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
public static <T, R> Collector<T, ?, R> chain(Function<List<?>, R> finalFinisher, Collector<T, ?, ?>... collectors) {
|
||||
final Function<List<Object>, List<?>> finisher = list -> {
|
||||
IntStream.range(0, collectors.length).forEach(
|
||||
i -> list.set(i, ((Function<Object, Object>) collectors[i].finisher()).apply(list.get(i)))
|
||||
);
|
||||
return list;
|
||||
};
|
||||
@SuppressWarnings("unchecked")
|
||||
Collector<T, List<Object>, R> result = Collector.of(() -> Arrays.stream(collectors)
|
||||
.map(Collector::supplier)
|
||||
.map(Supplier::get)
|
||||
.collect(Collectors.toList()),
|
||||
(list, e) -> {
|
||||
IntStream.range(0, collectors.length).forEach(
|
||||
i -> ((BiConsumer<Object, T>) collectors[i].accumulator()).accept(list.get(i), e)
|
||||
);
|
||||
},
|
||||
(l1, l2) -> {
|
||||
IntStream.range(0, collectors.length).forEach(
|
||||
i -> l1.set(i, ((BinaryOperator<Object>) collectors[i].combiner()).apply(l1.get(i), l2.get(i)))
|
||||
);
|
||||
return l1;
|
||||
},
|
||||
finisher.andThen(finalFinisher));
|
||||
return result;
|
||||
}
|
||||
}
|
90
src/main/java/com/annimon/Tester.java
Normal file
90
src/main/java/com/annimon/Tester.java
Normal file
@ -0,0 +1,90 @@
|
||||
package com.annimon;
|
||||
|
||||
|
||||
import com.annimon.stream.IntPair;
|
||||
import com.annimon.stream.IntStream;
|
||||
import com.annimon.stream.Stream;
|
||||
import com.annimon.stream.function.Predicate;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.DoubleStream;
|
||||
|
||||
|
||||
public final class Tester {
|
||||
|
||||
public static void main(String[] args) {
|
||||
double dv = DoubleStream.empty().flatMap(d -> DoubleStream.of(d)).iterator().nextDouble();
|
||||
System.out.println(dv);
|
||||
|
||||
int[] input = { 1, 2, 3, 4, 0, 5, 6, 7, 8, 9};
|
||||
int[] expected = { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4};
|
||||
|
||||
int[] actual = IntStream.of(input)
|
||||
.boxed()
|
||||
.chunkBy(x -> x == 0)
|
||||
.filter(list -> list.size() != 1 || list.get(0) == 0)
|
||||
.indexed()
|
||||
.sortBy(p -> -p.getFirst())
|
||||
.flatMap(p -> Stream.of(p.getSecond()))
|
||||
.mapToInt(i -> i)
|
||||
.peek(System.out::print)
|
||||
.toArray();
|
||||
System.out.println();
|
||||
System.out.println(Arrays.equals(actual, expected));
|
||||
|
||||
final String inputString = "aabaa";
|
||||
final int length = inputString.length();
|
||||
IntStream.range(0, length / 2)
|
||||
.mapToObj(i -> new char[] {
|
||||
inputString.charAt(i),
|
||||
inputString.charAt(length - i - 1)
|
||||
})
|
||||
.allMatch(arr -> arr[0] == arr[1]);
|
||||
|
||||
|
||||
/*double[][] input = {{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};
|
||||
Stream.of(input)
|
||||
.flatMap(arr -> IntStream.range(0, arr.length).mapToObj(i -> arr[i]))
|
||||
.forEach(System.out::println);*/
|
||||
|
||||
int[][] data = {
|
||||
{9, 95, 959,9585,9595},
|
||||
{0,7,8,9,79,80,87,89,98,797,798,879,897,899,979}
|
||||
};
|
||||
Stream.of(data)
|
||||
.map(arr -> IntStream.of(arr)
|
||||
.sorted(Tester::compare)
|
||||
.toArray()
|
||||
)
|
||||
.map(Arrays::toString)
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static int compare(int a, int b) {
|
||||
double rankA = rank(a);
|
||||
double rankB = rank(b);
|
||||
int cmp = Double.compare(rankA, rankB);
|
||||
if (cmp == 0) {
|
||||
return -Integer.compare(
|
||||
Integer.toString(a).length(),
|
||||
Integer.toString(b).length()
|
||||
);
|
||||
}
|
||||
return -cmp;
|
||||
}
|
||||
|
||||
private static double rank(int x) {
|
||||
String str = Integer.toString(x);
|
||||
double sum = str.charAt(0) - '0';
|
||||
for (int i = 1; i < str.length(); i++) {
|
||||
char ch0 = str.charAt(i - 1);
|
||||
char ch1 = str.charAt(i);
|
||||
double weight = 1.0 / (Math.pow(10, i + 1) * (double)(ch1 - '0' + 1));
|
||||
if (ch0 < ch1) {
|
||||
sum += weight;
|
||||
} else if (ch0 > ch1) {
|
||||
sum -= weight;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.annimon.inputstreamtostring;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public final class SourceLoader {
|
||||
|
||||
public static String readSourceBufferedReader(String name) throws IOException {
|
||||
InputStream is = SourceLoader.class.getResourceAsStream(name);
|
||||
if (is != null) return readStreamBufferedReader(is);
|
||||
|
||||
is = new FileInputStream(name);
|
||||
return readStreamBufferedReader(is);
|
||||
}
|
||||
|
||||
public static String readSourceBuffer(String name) throws IOException {
|
||||
InputStream is = SourceLoader.class.getResourceAsStream(name);
|
||||
if (is != null) return readStreamBuffer(is);
|
||||
|
||||
is = new FileInputStream(name);
|
||||
return readStreamBuffer(is);
|
||||
}
|
||||
|
||||
public static String readSourceBAOS(String name) throws IOException {
|
||||
InputStream is = SourceLoader.class.getResourceAsStream(name);
|
||||
if (is != null) return readStreamBAOS(is);
|
||||
|
||||
is = new FileInputStream(name);
|
||||
return readStreamBAOS(is);
|
||||
}
|
||||
|
||||
private static String readStreamBufferedReader(InputStream is) throws IOException {
|
||||
final StringBuilder text = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
text.append(line);
|
||||
text.append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
private static String readStreamBuffer(InputStream is) throws IOException {
|
||||
final StringBuilder text = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
|
||||
final char[] buffer = new char[1024];
|
||||
int readed;
|
||||
while ((readed = reader.read(buffer)) != -1) {
|
||||
text.append(buffer, 0, readed);
|
||||
}
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
private static String readStreamBAOS(InputStream is) throws IOException {
|
||||
final ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
final byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = is.read(buffer)) != -1) {
|
||||
result.write(buffer, 0, length);
|
||||
}
|
||||
is.close();
|
||||
return result.toString("UTF-8");
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.annimon.inputstreamtostring;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class SourceLoaderBechmarks {
|
||||
|
||||
private static final String RES_PATH = "/pipes.own";
|
||||
private static final String FILE_PATH = "C:/Users/aNNiMON/Documents/NetBeansProjects/_TUTORIALS/OwnLang/program.own";
|
||||
|
||||
@Benchmark
|
||||
public void bufferedReaderFromResources() throws IOException {
|
||||
SourceLoader.readSourceBufferedReader(RES_PATH);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void bufferFromResources() throws IOException {
|
||||
SourceLoader.readSourceBuffer(RES_PATH);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void baosFromResources() throws IOException {
|
||||
SourceLoader.readSourceBAOS(RES_PATH);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void bufferedReaderFromFile() throws IOException {
|
||||
SourceLoader.readSourceBufferedReader(FILE_PATH);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void bufferFromFile() throws IOException {
|
||||
SourceLoader.readSourceBuffer(FILE_PATH);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void baosFromFile() throws IOException {
|
||||
SourceLoader.readSourceBAOS(FILE_PATH);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(SourceLoaderBechmarks.class.getSimpleName())
|
||||
.warmupIterations(2)
|
||||
.measurementIterations(3)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
import com.annimon.stream.function.IntBinaryOperator;
|
||||
import com.annimon.stream.function.ToIntFunction;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class IndexedBenchmarks {
|
||||
private static final int SIZE = 10000000;
|
||||
private int[] input;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
input = new int[SIZE];
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
if (i < SIZE / 2) {
|
||||
input[i] = i;
|
||||
} else {
|
||||
input[i] = i - SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long indexedObject() {
|
||||
ToIntFunction<IndexedInt> mapper = idx -> idx.index() + idx.value();
|
||||
int index = 0;
|
||||
long sum = 0;
|
||||
for (int value : input) {
|
||||
IndexedInt idx = IndexedInt.of(value, index++);
|
||||
sum += mapper.applyAsInt(idx);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long indexedInts() {
|
||||
IntBinaryOperator mapper = (index, value) -> index + value;
|
||||
int index = 0;
|
||||
long sum = 0;
|
||||
for (int value : input) {
|
||||
sum += mapper.applyAsInt(index++, value);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(IndexedBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(3)
|
||||
.measurementIterations(5)
|
||||
.forks(2)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
24
src/main/java/com/annimon/streambenchmark/IndexedInt.java
Normal file
24
src/main/java/com/annimon/streambenchmark/IndexedInt.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
public final class IndexedInt {
|
||||
|
||||
private final int index;
|
||||
private final int value;
|
||||
|
||||
public static IndexedInt of(int index, int value) {
|
||||
return new IndexedInt(index, value);
|
||||
}
|
||||
|
||||
private IndexedInt(int index, int value) {
|
||||
this.index = index;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int index() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public int value() {
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class LsaStreamBenchmarks {
|
||||
|
||||
volatile int counts = 9999999;
|
||||
int[] array = new int[counts];
|
||||
volatile List<Integer> list = new ArrayList<>(counts);
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
populate(list);
|
||||
}
|
||||
|
||||
public void populate(List<Integer> list) {
|
||||
for (int i = 0; i < counts; i++) {
|
||||
if (i < counts / 2) {
|
||||
array[i] = i;
|
||||
list.add(i, i);
|
||||
} else {
|
||||
array[i] = i - counts;
|
||||
list.add(i, i - counts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int forSumIntegers() {
|
||||
int result = 0;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
result += array[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int forEachSumIntegers() {
|
||||
int result = 0;
|
||||
for (int value : array) {
|
||||
result += value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int forEachSumIntegersList() {
|
||||
int result = 0;
|
||||
for (int value : list) {
|
||||
result += value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int javaIntStreamSumIntegersWithReduce() {
|
||||
int result = java.util.stream.IntStream.of(array)
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int javaIntStreamSumIntegers() {
|
||||
int result = java.util.stream.IntStream.of(array)
|
||||
.sum();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int javaStreamSumIntegers() {
|
||||
int result = list.stream()
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int lsaStreamSumIntegers() {
|
||||
int result = com.annimon.stream.Stream.of(list)
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int lsaIntStreamSumIntegersWithReduce() {
|
||||
int result = com.annimon.stream.IntStream.of(array)
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int lsaIntStreamSumIntegers() {
|
||||
int result = com.annimon.stream.IntStream.of(array)
|
||||
.sum();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(LsaStreamBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(3)
|
||||
.measurementIterations(5)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import com.annimon.stream.function.Function;
|
||||
import com.annimon.stream.function.Supplier;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Param;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class SlidingWindowBenchmarks {
|
||||
|
||||
volatile int counts = 9999999;
|
||||
|
||||
@Param({"1", "5", "10", "20", "40", "80", "150", "400", "800"})
|
||||
public int windowSizeParam;
|
||||
|
||||
@Benchmark
|
||||
public long slidingWindowLinkedList() {
|
||||
return Stream.range(0, counts)
|
||||
.custom(new SlidingWindow<>(windowSizeParam, 10, LinkedList::new))
|
||||
.count();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long slidingWindowArrayDeque() {
|
||||
return Stream.range(0, counts)
|
||||
.custom(new SlidingWindow<>(windowSizeParam, 10, ArrayDeque::new))
|
||||
.count();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(SlidingWindowBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(3)
|
||||
.measurementIterations(5)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
|
||||
private static class SlidingWindow<T> implements Function<Stream<T>, Stream<List<T>>> {
|
||||
|
||||
private final int windowSize, stepWidth;
|
||||
private final Supplier<Queue<T>> supplier;
|
||||
|
||||
public SlidingWindow(int windowSize, int stepWidth, Supplier<Queue<T>> supplier) {
|
||||
this.windowSize = windowSize;
|
||||
this.stepWidth = stepWidth;
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<List<T>> apply(Stream<T> t) {
|
||||
final Iterator<? extends T> iterator = t.getIterator();
|
||||
return Stream.of(new Iterator<List<T>>() {
|
||||
private final Queue<T> queue = supplier.get();
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> next() {
|
||||
int i = queue.size();
|
||||
while (i < windowSize && iterator.hasNext()) {
|
||||
queue.offer(iterator.next());
|
||||
i++;
|
||||
}
|
||||
|
||||
// the elements that are currently in the queue are the elements of our current window
|
||||
List<T> list = new ArrayList<T>(queue);
|
||||
|
||||
// remove stepWidth elements from the queue
|
||||
int queueSize = queue.size();
|
||||
for (int j = 0; j < stepWidth && j < queueSize; j++) {
|
||||
queue.poll();
|
||||
}
|
||||
|
||||
// if the stepWidth is greater than the windowSize, skip (stepWidth - windowSize) elements
|
||||
for (int j = windowSize; j < stepWidth && iterator.hasNext(); j++) {
|
||||
iterator.next();
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@OutputTimeUnit(TimeUnit.SECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class StreamCountBenchmarks {
|
||||
volatile int counts = 9999999;
|
||||
volatile List<Integer> values = new ArrayList<>(counts);
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
populate(values);
|
||||
}
|
||||
|
||||
public void populate(List<Integer> list) {
|
||||
for (int i = 0; i < counts; i++) {
|
||||
if (i < counts / 2) {
|
||||
list.add(i, i);
|
||||
} else {
|
||||
list.add(i, i - counts);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long iteratorCountIntegers() {
|
||||
long result = 0;
|
||||
Iterator ite = values.iterator();
|
||||
while (ite.hasNext()) {
|
||||
result++;
|
||||
ite.next();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long fooEachCountIntegers() {
|
||||
long result = 0;
|
||||
for (Integer value : values) {
|
||||
result++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long parallelCountIntegers() {
|
||||
long result = values.parallelStream()
|
||||
.count();
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long sequentialCountIntegers() {
|
||||
long result = values.stream()
|
||||
.count();
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public long lsaCountIntegers() {
|
||||
long result = Stream.of(values.iterator())
|
||||
.count();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(StreamCountBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(3)
|
||||
.measurementIterations(5)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@OutputTimeUnit(TimeUnit.SECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class StreamMapBenchmarks {
|
||||
volatile int counts = 9999999;
|
||||
volatile List<Integer> values = new ArrayList<>(counts);
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
populate(values);
|
||||
}
|
||||
|
||||
public void populate(List<Integer> list) {
|
||||
for (int i = 0; i < counts; i++) {
|
||||
if (i < counts / 2) {
|
||||
list.add(i, i);
|
||||
} else {
|
||||
list.add(i, i - counts);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void iteratorMapIntegers() {
|
||||
long count = 0;
|
||||
Iterator<Integer> ite = values.iterator();
|
||||
while (ite.hasNext()) {
|
||||
count++;
|
||||
Integer value = ite.next();
|
||||
value = -value;
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fooEachMapIntegers() {
|
||||
long count = 0;
|
||||
for (Integer value : values) {
|
||||
count++;
|
||||
value = -value;
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void parallelMapIntegers() {
|
||||
values.parallelStream()
|
||||
.map(i -> -i)
|
||||
.count();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void sequentialMapIntegers() {
|
||||
values.stream()
|
||||
.map(i -> -i)
|
||||
.count();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void lsaMapIntegers() {
|
||||
Stream.of(values.iterator())
|
||||
.map(i -> -i)
|
||||
.count();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(StreamMapBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(3)
|
||||
.measurementIterations(5)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.annimon.streambenchmark;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class StreamSumBenchmarks {
|
||||
volatile int counts = 9999999;
|
||||
volatile List<Integer> values = new ArrayList<>(counts);
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
populate(values);
|
||||
}
|
||||
|
||||
public void populate(List<Integer> list) {
|
||||
for (int i = 0; i < counts; i++) {
|
||||
if (i < counts / 2) {
|
||||
list.add(i, i);
|
||||
} else {
|
||||
list.add(i, i - counts);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int iteratorSumIntegers() {
|
||||
int result = 0;
|
||||
Iterator<Integer> ite = values.iterator();
|
||||
while (ite.hasNext()) {
|
||||
result += ite.next();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int fooEachSumIntegers() {
|
||||
int result = 0;
|
||||
for (Integer value : values) {
|
||||
result += value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int parallelSumIntegers() {
|
||||
int result = values.parallelStream()
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int sequentialSumIntegers() {
|
||||
int result = values.stream()
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int lsaSumIntegers() {
|
||||
int result = Stream.of(values.iterator())
|
||||
.reduce(0, (a, b) -> a + b);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(StreamSumBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(0)
|
||||
.measurementIterations(1)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.annimon.stringempty;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@State(Scope.Benchmark)
|
||||
public class StringLengthBenchmarks {
|
||||
|
||||
String str = "";
|
||||
|
||||
@Benchmark
|
||||
public void lengthEqZero() {
|
||||
for(long k = 0; k < 1_000L; k++)
|
||||
for(long j = 0; j < 1_000L; j++)
|
||||
for(long i = 0; i < 1_000L; i++)
|
||||
if (str.length() == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void lengthGtZero() {
|
||||
for(long k = 0; k < 1_000L; k++)
|
||||
for(long j = 0; j < 1_000L; j++)
|
||||
for(long i = 0; i < 1_000L; i++)
|
||||
if (str.length() > 0) {
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void lengthLtZero() {
|
||||
for(long k = 0; k < 1_000L; k++)
|
||||
for(long j = 0; j < 1_000L; j++)
|
||||
for(long i = 0; i < 1_000L; i++)
|
||||
if (str.length() < 0) {
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void isEmpty() {
|
||||
for(long k = 0; k < 1_000L; k++)
|
||||
for(long j = 0; j < 1_000L; j++)
|
||||
for(long i = 0; i < 1_000L; i++)
|
||||
if (str.isEmpty()) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(StringLengthBenchmarks.class.getSimpleName())
|
||||
.warmupIterations(3)
|
||||
.measurementIterations(5)
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
1948
src/main/resources/pipes.own
Normal file
1948
src/main/resources/pipes.own
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user