StringBuiler&String.Format簡単なテスト
4185 ワード
basejunit.java
StringFormantAndStringBuildTest.java
テスト結果:
mehtod
time
junit_time(s)
log)
String.format
100
0.078
0
StringBuilder
100
0
0
StringBuffer
100
0
0
String+
100
0
0
String.format
1000
0.125
31
StringBuilder
1000
0.016
0
StringBuffer
1000
0
0
String+
1000
0.156
156
String.format
5000
0.172
109
StringBuilder
5000
0.032
32
StringBuffer
5000
0.031
31
String+
5000
16.609
16594
String.format
10000
0.235
172
StringBuilder
10000
0.062
47
StringBuffer
10000
0.047
47
String+
10000
~
129469
次はマルチスレッド同時の状況をテストして、ちょうどいくつかの機会を借りてマルチスレッドを勉強して、どうせよく知らない.
protected void configure(Class<?> clazz) {
PropertyConfigurator.configure(getLog4jConfiguration(clazz));
}
private Properties getLog4jConfiguration(Class<?> clazz) {
Properties prop = new Properties();
String className = clazz.getSimpleName().toLowerCase();
prop.setProperty(String.format("log4j.logger.%s",clazz.getPackage().getName()+"."+clazz.getSimpleName()), String.format("info,%s", className));
prop.setProperty(String.format("log4j.appender.%s",className), "org.apache.log4j.FileAppender");
prop.setProperty(String.format("log4j.appender.%s.layout",className), "org.apache.log4j.PatternLayout");
prop.setProperty(String.format("log4j.appender.%s.file",className), String.format("%s.log",clazz.getResource("").toString().replace("file:/", "")+"log/"+className));
prop.setProperty(String.format("log4j.appender.%s.layout.ConversionPattern",className), "%d %p [%c] - %m%n");
return prop;
}
StringFormantAndStringBuildTest.java
public class StringFormantAndStringBuildTest extends BaseJunit {
private Log log = null;
private long stamp=0;
int maxTime=100;
public StringFormantAndStringBuildTest(){
log = LogFactory.getLog(this.getClass());
super.configure(this.getClass());
}
@Before
public void step(){
}
@Test
public void StringFormatTest(){
stamp=System.currentTimeMillis();
StringBuilder testString=new StringBuilder();
Random random=new Random();
Object[] obj=new Object[maxTime];
for (int i=0 ;i<maxTime;i++){
testString.append("%s_");
obj[i]=random.nextLong();
}
log.info(String.format(testString.toString(),obj));
log.info("StringFromat stamp:"+(System.currentTimeMillis()-stamp));
}
@Test
public void StringBuilder(){
stamp=System.currentTimeMillis();
StringBuilder testString=new StringBuilder();
Random random=new Random();
for (int i=0 ;i<maxTime;i++){
testString.append(random.nextLong());
testString.append("-");
}
log.info(testString.toString());
log.info("StringBuilder stamp:"+(System.currentTimeMillis()-stamp));
}
@Test
public void StringBuffer(){
stamp=System.currentTimeMillis();
StringBuffer testString=new StringBuffer();
Random random=new Random();
for (int i=0 ;i<maxTime;i++){
testString.append(random.nextLong());
testString.append("-");
}
log.info(testString.toString());
log.info("StringBuffer stamp:"+(System.currentTimeMillis()-stamp));
}
@Test
public void String(){
stamp=System.currentTimeMillis();
String testString="";
Random random=new Random();
for (int i=0 ;i<maxTime;i++){
testString=testString+random.nextLong()+"-";
}
log.info(testString);
log.info("String stamp:"+(System.currentTimeMillis()-stamp));
}
}
テスト結果:
mehtod
time
junit_time(s)
log)
String.format
100
0.078
0
StringBuilder
100
0
0
StringBuffer
100
0
0
String+
100
0
0
String.format
1000
0.125
31
StringBuilder
1000
0.016
0
StringBuffer
1000
0
0
String+
1000
0.156
156
String.format
5000
0.172
109
StringBuilder
5000
0.032
32
StringBuffer
5000
0.031
31
String+
5000
16.609
16594
String.format
10000
0.235
172
StringBuilder
10000
0.062
47
StringBuffer
10000
0.047
47
String+
10000
~
129469
次はマルチスレッド同時の状況をテストして、ちょうどいくつかの機会を借りてマルチスレッドを勉強して、どうせよく知らない.