Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created September 10, 2014 16:18
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 komiya-atsushi/301ae973f7117f856b2f to your computer and use it in GitHub Desktop.
Save komiya-atsushi/301ae973f7117f856b2f to your computer and use it in GitHub Desktop.
Java 8 で導入された Base64 エンコーディング/デコーディングの機能と Commons Codec のそれとの性能比較をするプログラム。
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import java.util.Base64;
import java.util.function.Function;
/**
* Java 8 と Commons Codec の Base64 エンコーディング・デコーディング性能を比較します。
*
* 比較対象の Commons Codec と、パフォーマンス測定結果の統計処理のために Commons Math3 が必要になります。
*/
public class Base64Performance {
public static void main(String[] args) {
measureAndShowPerformance(300000, 40, 20);
measureAndShowPerformance(2000, 40, 20000);
}
static void measureAndShowPerformance(int loopTimes, int samplingTimes, int textLength) {
double[] encodingPerformanceByJava8 =
measureEncodingPerformance(
samplingTimes,
textLength,
data -> doEncodeByJava8(data, loopTimes));
double[] encodingPerformanceByCommonsCodec =
measureEncodingPerformance(
samplingTimes,
textLength,
data -> doEncodeByCommonsCodec(data, loopTimes));
double[] decodingPerformanceByJava8 =
measureDecodingPerformance(
samplingTimes,
textLength,
data -> doDecodeByJava8(data, loopTimes));
double[] decodingPerformanceByCommonsCodec =
measureDecodingPerformance(
samplingTimes,
textLength,
data -> doDecodeByCommonsCodec(data, loopTimes));
System.out.printf("# loop:%d, sampling:%d, text length:%d", loopTimes, samplingTimes, textLength);
System.out.println();
showPerformance("Java8 Encoding", encodingPerformanceByJava8);
showPerformance("Commons Codec Encoding", encodingPerformanceByCommonsCodec);
showPerformance("Java8 Decoding", decodingPerformanceByJava8);
showPerformance("Commons Codec Decoding", decodingPerformanceByCommonsCodec);
}
static void showPerformance(String label, double[] result) {
DescriptiveStatistics stats = new DescriptiveStatistics(result);
System.out.println("## " + label);
System.out.println("* 1st quantile [ms] : " + stats.getPercentile(25));
System.out.println("* median [ms] : " + stats.getPercentile(50));
System.out.println("* 3rd quantile [ms] : " + stats.getPercentile(75));
System.out.println();
}
static double[] measureEncodingPerformance(int samplingTimes, int textLength, Function<byte[], Long> f) {
double[] result = new double[samplingTimes];
byte[] data = generateText(textLength).getBytes();
for (int i = 0; i < samplingTimes; i++) {
result[i] = f.apply(data);
}
return result;
}
static double[] measureDecodingPerformance(int samplingTimes, int textLength, Function<byte[], Long> f) {
double[] result = new double[samplingTimes];
byte[] data = Base64.getEncoder().encode(generateText(textLength).getBytes());
for (int i = 0; i < samplingTimes; i++) {
result[i] = f.apply(data);
}
return result;
}
static String generateText(int textLength) {
String seed = "012ABCabcz";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (textLength + seed.length() - 1) / seed.length(); i++) {
sb.append(seed);
}
return sb.toString()
.substring(0, textLength);
}
// ----- Java 8 Standard Class Library
static long doEncodeByJava8(byte[] data, int times) {
Base64.Encoder encoder = Base64.getEncoder();
long begin = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
byte[] result = encoder.encode(data);
}
long end = System.currentTimeMillis();
return end - begin;
}
static long doDecodeByJava8(byte[] data, int times) {
Base64.Decoder decoder = Base64.getDecoder();
long begin = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
byte[] result = decoder.decode(data);
}
long end = System.currentTimeMillis();
return end - begin;
}
// ----- Commons Codec
static long doEncodeByCommonsCodec(byte[] data, int times) {
long begin = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
byte[] result = org.apache.commons.codec.binary.Base64.encodeBase64(data);
}
long end = System.currentTimeMillis();
return end - begin;
}
static long doDecodeByCommonsCodec(byte[] data, int times) {
long begin = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(data);
}
long end = System.currentTimeMillis();
return end - begin;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment