精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (9)
|
|
---|---|
作者 | 正文 |
发表时间:2011-08-31
最后修改:2011-09-01
java原生的replace,trim方法之间性能的巨大差别。 public class App { final static String teString = " 555555599999999999999999aaa sss ssaaaaaaaaaaaaaatasdtatastasrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "; public static void main( String[] args ) { int t = 100000; long start1 = System.nanoTime(); long stringUtilsReplaceTime = 0; long stringUtilsTrimTime = 0; long stringReplaceTime = 0; long stringTrimTime = 0; for(int i= 0;i<t;i++){ StringUtils.replace(teString,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","b"); } stringUtilsReplaceTime= System.nanoTime()-start1; start1 = System.nanoTime(); for(int i= 0;i<t;i++){ teString.replace("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "b"); } stringReplaceTime = System.nanoTime()-start1; start1 = System.nanoTime(); for(int i= 0;i<t;i++){ teString.trim(); } stringTrimTime = System.nanoTime()-start1; start1 = System.nanoTime(); for(int i= 0;i<t;i++){ StringUtils.trimWhitespace(teString); } stringUtilsTrimTime = System.nanoTime()-start1; System.out.println("stringUtilsReplaceTime:"+stringUtilsReplaceTime); System.out.println("stringReplaceTime :"+stringReplaceTime); System.out.println("stringUtilsTrimTime :"+stringUtilsTrimTime); System.out.println("stringTrimTime :"+stringTrimTime); } } 输出结果为: stringUtilsReplaceTime:293389595 stringReplaceTime :1019421816 stringUtilsTrimTime :176730378 stringTrimTime :3888271 public class App { final static String teString = " 555555599999999999999999aaa sss ssaaaaaaaaaaaaaatasdtatastasrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "; public static void main( String[] args ) { int t = 100; long start1 = System.nanoTime(); long stringUtilsReplaceTime = 0; long stringUtilsTrimTime = 0; long stringReplaceTime = 0; long stringTrimTime = 0; for(int i= 0;i<t;i++){ StringUtils.replace(teString,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","b"); } stringUtilsReplaceTime= System.nanoTime()-start1; start1 = System.nanoTime(); for(int i= 0;i<t;i++){ teString.replace("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "b"); } stringReplaceTime = System.nanoTime()-start1; start1 = System.nanoTime(); for(int i= 0;i<t;i++){ teString.trim(); } stringTrimTime = System.nanoTime()-start1; start1 = System.nanoTime(); for(int i= 0;i<t;i++){ StringUtils.trimWhitespace(teString); } stringUtilsTrimTime = System.nanoTime()-start1; System.out.println("stringUtilsReplaceTime:"+stringUtilsReplaceTime); System.out.println("stringReplaceTime :"+stringReplaceTime); System.out.println("stringUtilsTrimTime :"+stringUtilsTrimTime); System.out.println("stringTrimTime :"+stringTrimTime); } } 结果为: stringUtilsReplaceTime:94864540 stringReplaceTime :8341259 stringUtilsTrimTime :953821 stringTrimTime :57856 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-08-31
の,兄弟,这个测试有点不准确啊
1、有没有试过测试方法中将StringUtils.replace()和replace()的测试顺序对调下,输出下结果? 2、有没有考虑jvm刚启动时需要预热的情况? |
|
返回顶楼 | |
发表时间:2011-09-01
4-5倍的性能差异应该是在预料之中的
|
|
返回顶楼 | |
发表时间:2011-09-01
靠,一个10000次,一个1000次,能一样吗?
|
|
返回顶楼 | |
发表时间:2011-09-01
opal 写道 靠,一个10000次,一个1000次,能一样吗?
+1 |
|
返回顶楼 | |
发表时间:2011-09-01
opal 写道 靠,一个10000次,一个1000次,能一样吗?
已经修改,悲剧。 |
|
返回顶楼 | |
发表时间:2011-09-01
这说明在数据执行次数比较多的超过10000次,java原生的replace性能会急剧下降,而apache的replace才能显现出优势来,呵呵。
|
|
返回顶楼 | |
发表时间:2011-09-01
对同一个字符串执行10000次相同的替换。。。
|
|
返回顶楼 | |
发表时间:2011-09-01
一个支持正则,一个不支持,性能上当然有差异。
|
|
返回顶楼 | |
发表时间:2011-09-01
我自己试了一下,如果数据执行次数比较少,少于200次,java原生的replace那个执行时间反而少,而apache的replace的执行时间却比较多一些,呵呵。
到底实际应用中,哪一种比较常见呢?俺想不到什么时候需要对一个字符串操作很多次。是不是改成对多个不同的字符串操作来测试才会比较有意义呢? |
|
返回顶楼 | |