Created
July 22, 2012 01:33
-
-
Save kenota/3157873 to your computer and use it in GitHub Desktop.
builder vs buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.binarybuffer.test.bufferbuilder; | |
import org.apache.commons.math3.stat.descriptive.StatisticalSummary; | |
import org.apache.commons.math3.stat.descriptive.SummaryStatistics; | |
import org.junit.Test; | |
/** | |
* Unit test for simple App. | |
*/ | |
public class PefromanceTest { | |
private double executeAndMeasure(Runnable r, long times) { | |
long startTime = System.nanoTime(); | |
for (long i = 0; i < times; i++) { | |
r.run(); | |
} | |
return (System.nanoTime() - startTime)/1000000000; | |
} | |
@Test | |
public void test() { | |
long interations = 50000000; | |
long executions = 30; | |
SummaryStatistics bufferStat = new SummaryStatistics(); | |
SummaryStatistics builderStat = new SummaryStatistics(); | |
for (int i = 0; i < executions; i++) { | |
System.out.println("Doing round " + i); | |
builderStat.addValue(executeAndMeasure(stringBuilder, interations)); | |
bufferStat.addValue(executeAndMeasure(stringBuffer, interations)); | |
} | |
System.out.println(String.format("Builder: Mean %f Std: %f ", | |
builderStat.getMean(), | |
builderStat.getStandardDeviation())); | |
System.out.println(String.format("Buffer : Mean %f Std: %f ", | |
bufferStat.getMean(), | |
bufferStat.getStandardDeviation())); | |
} | |
private Runnable stringBuffer = new Runnable() { | |
public void run() { | |
StringBuffer sb = new StringBuffer(); | |
sb.append("someString"); | |
sb.append(100); | |
sb.append(1l); | |
sb.append('a'); | |
} | |
}; | |
private Runnable stringBuilder = new Runnable() { | |
public void run() { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("someString"); | |
sb.append(100); | |
sb.append(1l); | |
sb.append('a'); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment