String.replaceAll比自己完成工作慢得多 [英] String.replaceAll is considerably slower than doing the job yourself

查看:423
本文介绍了String.replaceAll比自己完成工作慢得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段旧代码,用于在字符串中执行查找和替换标记。



它从收到的地图对,迭代它们,对于每个对,迭代目标字符串,从中查找使用 indexOf(),并将其替换为的值为。它完成了 StringBuffer 的所有工作,最终返回 String



我用这一行替换了那段代码: replaceAll([,。] *,);

我跑了一些比较性能测试。

当比较 1,000,000 迭代时,我得到了这个:


旧代码:1287ms

新代码:4605ms


3倍! / p>

然后尝试将其替换为3次调用替换

replace(,,);

replace(。,);

replace(,);



这导致以下结果:


旧代码:1295

新代码:3524


2倍!



知道为什么替换 replaceAll 是如此低效?我可以做些什么来加快速度吗?






编辑:感谢所有答案 - 主要问题确实是 [,。 ] * 没有做我想做的事。将其更改为 [,。 ] + 几乎等于非基于正则表达式的解决方案的性能。
使用预编译的正则表达式有所帮助,但是很少。 (这是一个非常适用于我的问题的解决方案。



测试代码:

用正则表达式替换字符串:[,。] *

用正则表达式替换字符串:[,。] +

用正则表达式替换字符串:[,。] +和预编译模式

解决方案

虽然使用正则表达式会对某些性能产生影响,但它不应该那么糟糕。



请注意,使用 String.replaceAll() 将编译正则表达式每次



您可以通过明确使用 模式 对象:

 模式p = Pattern.compile([,。 ] +); 

//仅重复以下部分:
字符串输出= p.matcher(输入).replaceAll();

还要注意使用 + 而不是 * 避免替换空字符串,因此也可能加快进程。


I have an old piece of code that performs find and replace of tokens within a string.

It receives a map of from and to pairs, iterates over them and for each of those pairs, iterates over the target string, looks for the from using indexOf(), and replaces it with the value of to. It does all the work on a StringBuffer and eventually returns a String.

I replaced that code with this line: replaceAll("[,. ]*", "");
And I ran some comparative performance tests.
When comparing for 1,000,000 iterations, I got this:

Old Code: 1287ms
New Code: 4605ms

3 times longer!

I then tried replacing it with 3 calls to replace:
replace(",", "");
replace(".", "");
replace(" ", "");

This resulted with the following results:

Old Code: 1295
New Code: 3524

2 times longer!

Any idea why replace and replaceAll are so inefficient? Can I do something to make it faster?


Edit: Thanks for all the answers - the main problem was indeed that [,. ]* did not do what I wanted it to do. Changing it to be [,. ]+ almost equaled the performance of the non-Regex based solution. Using a pre-compiled regex helped, but was marginal. (It is a solution very applicable for my problem.

Test code:
Replace string with Regex: [,. ]*
Replace string with Regex: [,. ]+
Replace string with Regex: [,. ]+ and Pre-Compiled Pattern

解决方案

While using regular expressions imparts some performance impact, it should not be as terrible.

Note that using String.replaceAll() will compile the regular expression each time you call it.

You can avoid that by explicitly using a Pattern object:

Pattern p = Pattern.compile("[,. ]+");

// repeat only the following part:
String output = p.matcher(input).replaceAll("");

Note also that using + instead of * avoids replacing empty strings and therefore might also speed up the process.

这篇关于String.replaceAll比自己完成工作慢得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆