精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-01-10
C/C++字符串拼接测试运行结果:
Release version. TEST_TIME: 90000 C language memcpy append Used: 0 ms. string length: 630000 std::string += operator Used: 15 ms. string length: 630000 std::string append Used: 16 ms. string length: 630000 std::ostringstream << Used: 16 ms. string length: 630000 capacity: 630015 size: 0 hava resize(PREALLOCATE_SIZE) std::string += operator Used: 0 ms. string length: 630000 MFC CString append Used: 63 ms. string length: 630000 MFC CString operator append Used: 62 ms. string length: 630000 c string function strcat: Used: 32203 ms. string length: 630000 ---------Statistics informations(sorting ascendent)-------
sort order: 1 item: C language memcpy append used: 0 ms.
sort order: 2 item: hava resize(PREALLOCATE_SIZE) std::string += operator used: 0 ms.
sort order: 3 item: std::string += operator used: 15 ms.
sort order: 4 item: std::string append used: 16 ms.
sort order: 5 item: std::ostringstream << used: 16 ms.
sort order: 6 item: MFC CString operator append used: 62 ms.
sort order: 7 item: MFC CString append used: 63 ms.
sort order: 8 item: c string function strcat: used: 32203 ms.
---------------------------------------------------------- Press any key to continue . . . Java的测试代码段:void testJavaStringPerformance() { final int TEST_TIMES = 90000; //String String str = new String();
System.out.println("The testing is running, please wait..."); long start = System.currentTimeMillis(); for (int i=0; i<TEST_TIMES; i++) { str += "cppmule"; } long strUsed = System.currentTimeMillis() - start; System.out.println("strUsed: " + strUsed + " ms.");
//StringBuffer start = 0; StringBuffer strBuffer = new StringBuffer(); start = System.currentTimeMillis(); for (int i=0; i<TEST_TIMES; i++) { strBuffer.append("cppmule"); } long strBufferUsed = System.currentTimeMillis() - start; System.out.println("StringBuffer append: " + strBufferUsed + " ms.");
//StringBuilder start = 0; StringBuilder strBuilder = new StringBuilder(); start = System.currentTimeMillis(); for (int i=0; i<TEST_TIMES; i++) { strBuilder.append("cppmule"); } long strBuilderUsed = System.currentTimeMillis() - start; System.out.println("StringBuilder append: " + strBuilderUsed + " ms."); System.out.println("Times: " + TEST_TIMES); } Java字符串拼接运行结果:
The testing is running, please wait... strUsed: 443141 ms. StringBuffer append: 15 ms. StringBuilder append: 31 ms. Times: 90000
Author: By: cppmule Email: cppmule@gmail.com
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-01-10
StringBuffer 竟然比 StringBuilder快一倍。这是怎么个情况?
StringBuffer可是线程安全的,虽然程序没用到多线程,但怎么也比StringBuilder要多耗一点时间吧。。 |
|
返回顶楼 | |
发表时间:2011-01-11
这个好像没有什么可比性。除掉调式信息+server模式再试下!
|
|
返回顶楼 | |
发表时间:2011-01-11
StringBuffer append: 48 ms.
StringBuilder append: 14 ms. Times: 90000 |
|
返回顶楼 | |
发表时间:2011-01-20
The testing is running, please wait...
strUsed: 57125 ms. StringBuffer append: 16 ms. StringBuilder append: 0 ms. Times: 90000 |
|
返回顶楼 | |
发表时间:2011-01-21
你直接用加号 +++++ 都比StringBuffer 快 stringbuffer是线程安全的吧
|
|
返回顶楼 | |
发表时间:2011-01-21
我对你测试结果标识怀疑.你等等 啊
|
|
返回顶楼 | |
发表时间:2011-01-21
我记得+也挺快的么..估计写道for循环里..jdk不做优化了..
|
|
返回顶楼 | |
发表时间:2011-01-21
ansjsun 写道 我记得+也挺快的么..估计写道for循环里..jdk不做优化了..
“记得”就是感觉吧 打开class文件看看+的实现就知道他们之间的区别了! 区别:1 对象创建个数 2 线程安全 |
|
返回顶楼 | |
发表时间:2011-01-21
偶测过,stringbuffer不可能有stringbuilder快的,在7次拼装一下+都比stringbuffer快。
|
|
返回顶楼 | |