使用 StringUtils 方法比 Java 方法运行得更快?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7907688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:56:38  来源:igfitidea点击:

Using StringUtils methods runs faster than Java methods?

javastringapache-commons

提问by Renato Dinhani

I want to know if the methods from StringUtils from Apache Commons Lang library runs faster than Java String methods.

我想知道 Apache Commons Lang 库中 StringUtils 的方法是否比 Java String 方法运行得更快。

I know this is micro-optimization, but it's necessary because methods will be executed millions times.

我知道这是微优化,但这是必要的,因为方法将被执行数百万次。

Methods that using regex in Java like split()or String comparison with equals().

在 Java 中使用正则表达式的方法 likesplit()或 String 与equals().

采纳答案by Renato Dinhani

StringUtils#splitis faster than String#split.

StringUtils#split比 快String#split

String#equalsis faster than StringUtils#equals.

String#equals比 快StringUtils#equals

回答by Mark Byers

Both the methods in the Java Stringclass and the methods in StringUtilsare highly optimised for the specific tasks they were designed to solve. Different methods are designed to solve slightly different tasks. Try to pick the method that most closely matches the task you are trying to solve, and it's likely you will get very good performance.

JavaString类中的方法和其中的方法StringUtils都针对它们旨在解决的特定任务进行了高度优化。不同的方法旨在解决略有不同的任务。尝试选择与您要解决的任务最匹配的方法,很可能会获得非常好的性能。

If there are two possible approaches that look equally good, and it's vital to get every last bit of performance, then measure it! Time the performance of both approaches on your actual data.

如果有两种可能的方法看起来同样出色,并且获得最后一点性能至关重要,那么请衡量它!对两种方法在您的实际数据上的性能进行计时。

But also remember that it's often cheaper to buy a faster computer instead of spending hours of development time micro-optimizing your code to get it to run fast on a slow machine.

但也要记住,购买更快的计算机通常更便宜,而不是花费数小时的开发时间来微优化您的代码以使其在慢速机器上快速运行。

回答by Sled

Well it probably depends on each individual method. Most of the methods represent very basic code that people often have to write by hand such as

好吧,这可能取决于每种方法。大多数方法代表了人们经常必须手工编写的非常基本的代码,例如

return s1 == s2 || (s1 == null && s2 == null) || (s1 != null && s1.equals( s2 ) );

This code would very likely be optimised to the same thing by the hotspot compiler removing even the method call. I would guess that the only savings in time you'll find are in writing.

通过热点编译器甚至删除方法调用,此代码很可能会优化为相同的内容。我猜你能找到的唯一节省时间的是写作。

As suggested elsewhere, I would just recommend benchmarking the methods you are interesting in yourself use System.currentTimeMillis()around a loop that calls the method on some randomstrings; random because you don't want it optimised away.

正如其他地方所建议的那样,我只建议对您自己感兴趣的方法进行基准测试,该方法System.currentTimeMillis()围绕在一些随机字符串上调用该方法的循环进行;随机,因为你不希望它被优化掉。

回答by MarianP

I do not think it is anyhow optimized, Apache Commons contains 'helper utilities', meaning it it is more efficient for development not for run-time performance efficiency.

我认为它无论如何都没有优化,Apache Commons 包含“辅助实用程序”,这意味着它对于开发效率更高,而不是运行时性能效率更高

The performance may also change with JVM used.

性能也可能随着JVM 的使用而改变。

I can understand, that you worry about performance with large number of executions or big data. But that does not mean that the micro-optimization fallacydoes not apply with regards to this. It is certainly more efficientto spend more time thinking about and trying out other ways to improve performance of such rudimentary tasks (running not in one but in as many threads as there are CPUs on the machine, etc.). It is wiser to leave such low level stuff for the JVM. The just-in-time-compiler is usually much smarter than any developer in my experience.

我可以理解,您担心大量执行或大数据的性能。但这并不意味着微观优化谬误不适用于此。花更多时间思考和尝试其他方法来提高此类基本任务的性能(不是在一个线程中运行,而是在与机器上的 CPU 数量一样多的线程中运行,等等),无疑会更有效率。为 JVM 留下如此低级的东西是更明智的。根据我的经验,即时编译器通常比任何开发人员都聪明得多。

If you really want to go that way, you should test it. it should not be that difficult and in case the performance depends on data, you might have the right one to test it with.

如果你真的想走那条路,你应该测试一下。这应该不难,如果性能取决于数据,您可能有合适的人来测试它。

回答by jtoberon

First, have you set performance criteria? How much performance are you looking to gain?

首先,您是否设定了绩效标准?您希望获得多少性能?

Second, have you profiled your application as a whole under a realistic load? The first thing you should do is make sure that you're even working on the right part of the code.

其次,您是否在实际负载下对整个应用程序进行了概要分析?您应该做的第一件事是确保您甚至在处理代码的正确部分。

Regardless, your question is too vague to be answered here. You need to provide more information about the data set you're working with, and also about the operations you're actually doing on the data. For example, one trick for optimizing away String.equals is to intern the String instances. You'll pay a pretty high upfront cost for creating the instances in the first place, but later the equals should return true based on ==before it bothers to do a full comparison. You'd need to decide whether to apply this optimization based on the data set in question and what you're doing with the data.

无论如何,您的问题太模糊,无法在这里回答。您需要提供有关您正在使用的数据集以及您实际对数据进行的操作的更多信息。例如,优化 String.equals 的一个技巧是实习 String 实例。您首先需要为创建实例支付相当高的前期成本,但稍后 equals 应该==在它费心进行全面比较之前返回 true 。您需要根据相关数据集以及您对数据的处理方式来决定是否应用此优化。

回答by Steve J

Why are you looking for faster? Don't you have a performance requirement? That will define whether you are fast enough. As long as you meet the requirement, then move on to the next problem. The question of "Which is faster?" implies you haven't tried either. Pick one, check that your performance requirement is met, and only check the other if the requirement is not met.

你为什么要寻找更快?你没有性能要求吗?这将定义你是否足够快。只要满足要求,就继续下一个问题。“哪个更快?”的问题 意味着你也没有尝试过。选择一个,检查是否满足您的性能要求,如果不满足要求,则仅检查另一个。

回答by user207421

They're not in competition. StringUtilsisn't a replacement for methods in the Stringclass, it is a collection of common idioms on the next higher layer. StringUtilscalls Stringin most cases. It can't do anything else, unless it used reflection, which it doesn't.

他们不是在竞争。StringUtils不是String类中方法的替代品,它是下一个更高层的常见习语的集合。大多数情况下StringUtils调用String。它不能做任何其他事情,除非它使用反射,但它没有。