Skip to content

Instantly share code, notes, and snippets.

@aoetk
Last active August 29, 2015 13:58
Show Gist options
  • Save aoetk/10400528 to your computer and use it in GitHub Desktop.
Save aoetk/10400528 to your computer and use it in GitHub Desktop.
文字列連結のブログエントリのコードhttp://d.hatena.ne.jp/aoe-tk/20140409/1397059399
package aoetk.stringjoin;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class StringJoinBench {
static String[] strarray;
static List<String> strlist;
static {
strarray = IntStream.range(0, 1000).map(i -> (i % 9) + 1)
.mapToObj(len -> IntStream.range(0, len).mapToObj(i -> "a").collect(Collectors.joining()))
.toArray(size -> new String[size]);
strlist = Arrays.asList(strarray);
}
static CharBuffer directBuffer = ByteBuffer.allocateDirect(15990).asCharBuffer();
public static void main(String[] args) {
bench("charBufferJoin", StringJoinBench::charBufferJoin);
bench("directBufferJoin", StringJoinBench::directBufferJoin);
bench("stringJoin", StringJoinBench::stringJoin);
bench("stringJoiner", StringJoinBench::stringJoiner);
bench("streamListJoin3", StringJoinBench::streamListJoin3);
bench("streamListParallelJoin3", StringJoinBench::streamListParallelJoin3);
bench("stringBuilderJoin", StringJoinBench::stringBuilderJoin);
bench("stringBuilderJoinMem", StringJoinBench::stringBuilderJoinMem);
bench("stringBuilderFuckingJoin", StringJoinBench::stringBuilderFuckingJoin);
}
public static String charBufferJoin() {
CharBuffer buffer = CharBuffer.allocate(7995);
buffer.put('[');
for (int i = 0; i < strarray.length; ++i) {
if (i != 0) {
buffer.put(',').put('[');
}
buffer.put(strarray[i]).put(']');
}
buffer.flip();
return buffer.toString();
}
public static String directBufferJoin() {
directBuffer.put('[');
for (int i = 0; i < strarray.length; ++i) {
if (i != 0) {
directBuffer.put(',').put('[');
}
directBuffer.put(strarray[i]).put(']');
}
directBuffer.flip();
return directBuffer.toString();
}
public static String stringJoin(){
return "[" + String.join("],[", strarray) + "]";
}
public static String stringJoiner(){
StringJoiner sj = new StringJoiner("],[", "[", "]");
for(int i = 0; i < strarray.length; ++i){
sj.add(strarray[i]);
}
return sj.toString();
}
public static String stringBuilderJoin(){
StringBuilder s = new StringBuilder("[");
for(int i = 0; i < strarray.length; ++i){
if(i != 0){
s.append("],[");
}
s.append(strarray[i]);
}
s.append("]");
return s.toString();
}
public static String stringBuilderJoinMem(){
StringBuilder s = new StringBuilder(9000).append("[");
for(int i = 0; i < strarray.length; ++i){
if(i != 0){
s.append("],[");
}
s.append(strarray[i]);
}
s.append("]");
return s.toString();
}
public static String stringBuilderFuckingJoin(){
StringBuilder s = new StringBuilder();
for(int i = 0; i < strarray.length; ++i){
if(i != 0){
s.append(",");
}
s.append("[" + strarray[i] + "]");
}
return s.toString();
}
public static String streamListJoin3(){
return strlist.stream()
.collect(Collectors.joining("],[", "[", "]"));
}
public static String streamListParallelJoin3(){
return strlist.parallelStream()
.collect(Collectors.joining("],[", "[", "]"));
}
public static void bench(String name, Supplier<String> proc){
bench(name, 50_000, proc);
}
public static void bench(String name, int count, Supplier<String> proc){
if(!stringJoin().equals(proc.get())) throw new RuntimeException(name + " is defferent with array join.");
for(int i = 0; i < 100; ++i){
proc.get();
}
long s = System.currentTimeMillis();
for(int i = 0; i < count; ++i){
proc.get();
}
System.out.printf("%s:%dms%n", name, System.currentTimeMillis() - s);
}
}
package aoetk.stringjoin;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class StringJoinBenchTest {
private static String result;
@Before
public void setUp() throws Exception {
result = StringJoinBench.stringJoin();
System.out.println("Test string length = " + result.length());
}
@org.junit.Test
public void testStringJoiner() throws Exception {
assertThat(StringJoinBench.stringJoiner(), is(result));
}
@org.junit.Test
public void testStringBuilderJoin() throws Exception {
assertThat(StringJoinBench.stringBuilderJoin(), is(result));
}
@org.junit.Test
public void testStringBuilderJoinMem() throws Exception {
assertThat(StringJoinBench.stringBuilderJoinMem(), is(result));
}
@org.junit.Test
public void testStringBuilderFuckingJoin() throws Exception {
assertThat(StringJoinBench.stringBuilderFuckingJoin(), is(result));
}
@org.junit.Test
public void testStreamListJoin3() throws Exception {
assertThat(StringJoinBench.streamListJoin3(), is(result));
}
@org.junit.Test
public void testStreamListParallelJoin3() throws Exception {
assertThat(StringJoinBench.streamListParallelJoin3(), is(result));
}
@Test
public void testCharBufferJoin() throws Exception {
assertThat(StringJoinBench.charBufferJoin(), is(result));
}
@Test
public void testDirectBufferJoin() {
assertThat(StringJoinBench.directBufferJoin(), is(result));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment