Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package org.osgl.util;
import org.osgl.$;
import org.osgl.util.S;
public class SConcatBenchmark {
public static void main(String[] args) {
final int WARMUP = 1000000;
final int LOOP = 100000000;
String result = null;
String s1 = S.random(4), s2 = S.random(10), s3 = S.random(8);
long ts = $.ms();
for (int i = 0; i < WARMUP; ++i) {
result = concat1(s1, s2, s3);
}
for (int i = 0; i < WARMUP; ++i) {
result = concat2(s1, s2, s3);
}
for (int i = 0; i < WARMUP; ++i) {
result = concat3(s1, s2, s3);
}
for (int i = 0; i < WARMUP; ++i) {
result = concat4(s1, s2, s3);
}
System.out.println("it takes " + ($.ms() - ts) + "ms to finish warm up");
ts = $.ms();
for (int i = 0; i < LOOP; ++i) {
result = concat1(s1, s2, s3);
}
long t1 = $.ms() - ts;
ts = $.ms();
for (int i = 0; i < LOOP; ++i) {
result = concat2(s1, s2, s3);
}
long t2 = $.ms() - ts;
ts = $.ms();
for (int i = 0; i < LOOP; ++i) {
result = concat3(s1, s2, s3);
}
long t3 = $.ms() - ts;
ts = $.ms();
for (int i = 0; i < LOOP; ++i) {
result = concat4(s1, s2, s3);
}
long t4 = $.ms() - ts;
System.out.println("String +: " + t1);
System.out.println("StringBuilder: " + t2);
System.out.println("osgl.S.concat: " + t3);
System.out.println("osgl.StrBuffer: " + t4);
System.out.println(result);
}
private static String concat1(String s1, String s2, String s3) {
return s1 + s2 + s3;
}
private static String concat2(String s1, String s2, String s3) {
return new StringBuilder(s1).append(s2).append(s3).toString();
}
private static String concat3(String s1, String s2, String s3) {
return S.concat(s1, s2, s3);
}
private static String concat4(String s1, String s2, String s3) {
return S.buffer(s1).a(s2).a(s3).toString();
}
}
@greenlaw110

This comment has been minimized.

Show comment Hide comment
@greenlaw110

greenlaw110 Jun 16, 2017

Running the above code on osgl-tool-1.2.0 shows the following result:

it takes 357ms to finish warm up
String +: 3164
StringBuilder: 1953
osgl.S.concat: 4513
osgl.StrBuffer: 3832
Ok_~Dbc@tbLS@a0JeX!o4p
Owner

greenlaw110 commented Jun 16, 2017

Running the above code on osgl-tool-1.2.0 shows the following result:

it takes 357ms to finish warm up
String +: 3164
StringBuilder: 1953
osgl.S.concat: 4513
osgl.StrBuffer: 3832
Ok_~Dbc@tbLS@a0JeX!o4p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment