Skip to content

Instantly share code, notes, and snippets.

@stokito
Last active August 17, 2019 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stokito/7671ecd80e786a93f91c16ca9d173f3a to your computer and use it in GitHub Desktop.
Save stokito/7671ecd80e786a93f91c16ca9d173f3a to your computer and use it in GitHub Desktop.
package com.github.stokito.experiments;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.openjdk.jmh.annotations.Mode.Throughput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
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.infra.Blackhole;
/*
Benchmark Mode Cnt Score Error Units
StreamSortBench.testCloneSort thrpt 5 861.309 ± 71.133 ops/ms
StreamSortBench.testStreamSort thrpt 5 768.630 ± 29.832 ops/ms
*/
@State(Scope.Thread)
public class StreamSortBench {
private static final String[] COLUMNS = {
"id",
"merchant_id",
"public_key",
"registration_date",
"reserve_fee",
"secret_key",
"status_acknowledged",
"marketplace_id",
"payout_interval_id",
"merchant_status",
"newsletter_subscribed",
"logo",
"firts_name",
"last_name",
"company_id",
"wallet_account_code",
"unavailable_message",
"wallet_kyc_check_status"
};
@Setup(Level.Trial)
public void setup() {
}
@Benchmark
@BenchmarkMode(Throughput)
@OutputTimeUnit(MILLISECONDS)
public void testStreamSort(Blackhole bh) {
StringBuilder sb = new StringBuilder(1024);
Arrays.stream(COLUMNS)
.sorted(String::compareTo)
.forEachOrdered(columnName -> sb.append( "column`" ).append( columnName ).append( '`' ));
bh.consume(sb.toString());
}
@Benchmark
@BenchmarkMode(Throughput)
@OutputTimeUnit(MILLISECONDS)
public void testCloneSort(Blackhole bh) {
StringBuilder sb = new StringBuilder(1024);
String[] alphabeticalColumns = COLUMNS.clone();
Arrays.sort( alphabeticalColumns, String::compareTo);
for ( String column : alphabeticalColumns ) {
String columnName = column;
sb.append( "column`" ).append( columnName ).append( '`' );
}
bh.consume(sb.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment