Skip to content

Commit

Permalink
Small enhancements to benchmarks and EmojiUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
felldo committed Nov 27, 2023
1 parent b77573d commit aa343f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/src/jmh/java/benchmark/EmojiManagerBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fellbaum.jemoji.Emoji;
import net.fellbaum.jemoji.EmojiManager;
import net.fellbaum.jemoji.EmojiManagerTest;
import net.fellbaum.jemoji.IndexedEmoji;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.OutputTimeUnit;

Expand Down Expand Up @@ -66,6 +67,11 @@ public List<Emoji> extractEmojisInOrder() {
return EmojiManager.extractEmojisInOrder(TEXT);
}

@Benchmark
public List<IndexedEmoji> extractEmojisInOrderWithIndex() {
return EmojiManager.extractEmojisInOrderWithIndex(TEXT);
}

@Benchmark
public List<Emoji> extractEmojisInOrderOnlyEmojisLengthDescending() {
return EmojiManager.extractEmojisInOrder(EmojiManagerTest.ALL_EMOJIS_STRING);
Expand Down
57 changes: 57 additions & 0 deletions lib/src/jmh/java/benchmark/excluded/CodePointBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package benchmark.excluded;

import benchmark.EmojiManagerBenchmark;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class CodePointBenchmark {

private static final String TEXT = new BufferedReader(new InputStreamReader(Objects.requireNonNull(EmojiManagerBenchmark.class.getClassLoader().getResourceAsStream("ExampleTextFileWithEmojis.txt"))))
.lines().collect(Collectors.joining("\n"));

@Param("\uD83D\uDC4D\uD83C\uDFFC")
private String alias;

/*
Benchmark (alias) Mode Cnt Score Error Units
CodePointBenchmark.codePointArrayLength 👍🏼 avgt 10 20,336 ± 0,062 ns/op
CodePointBenchmark.codePointCount 👍🏼 avgt 10 2,838 ± 0,011 ns/op
CodePointBenchmark.codePointStreamCount 👍🏼 avgt 10 22,974 ± 0,236 ns/op
CodePointBenchmark.codepointsStreamToArray 👍🏼 avgt 10 1707,719 ± 11,200 us/op
CodePointBenchmark.codepointsArrayForIndex 👍🏼 avgt 10 610,829 ± 112,007 us/op
CodePointBenchmark.codepointsStreamToArrayIterator 👍🏼 avgt 10 1604,416 ± 7,238 us/op
*/

@Benchmark
public int codePointArrayLength() {
return alias.codePoints().toArray().length;
Expand All @@ -24,4 +47,38 @@ public long codePointStreamCount() {
public int codePointCount() {
return alias.codePointCount(0, alias.length());
}

@Benchmark
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public int[] codepointsStreamToArray() {
return TEXT.codePoints().toArray();
}

@Benchmark
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public int[] codepointsStreamToArrayIterator() {
int[] codePoints = new int[getCodePointCount(TEXT)];
int j = 0;
for (final int codepoint : (Iterable<Integer>) TEXT.codePoints()::iterator) {
codePoints[j++] = codepoint;
}
return codePoints;
}

@Benchmark
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public int[] codepointsArrayForIndex() {
int[] codePoints = new int[getCodePointCount(TEXT)];
int j = 0;
for (int i = 0; i < TEXT.length(); ) {
final int codePoint = TEXT.codePointAt(i);
codePoints[j++] = codePoint;
i += Character.charCount(codePoint);
}
return codePoints;
}

private static int getCodePointCount(final String text) {
return text.codePointCount(0, text.length());
}
}
8 changes: 5 additions & 3 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

class EmojiUtils {

public static int getCodePointCount(String string) {
private EmojiUtils() {}

public static int getCodePointCount(final String string) {
return string.codePointCount(0, string.length());
}

Expand All @@ -29,8 +31,8 @@ public static <K, V> Optional<V> findEmojiByEitherAlias(final Map<K, V> map, fin
return Optional.empty();
}

public static int[] stringToCodePoints(String text) {
int[] codePoints = new int[getCodePointCount(text)];
public static int[] stringToCodePoints(final String text) {
final int[] codePoints = new int[getCodePointCount(text)];
int j = 0;
for (int i = 0; i < text.length();) {
final int codePoint = text.codePointAt(i);
Expand Down

0 comments on commit aa343f4

Please sign in to comment.