简体   繁体   English

为什么 Java 不在 javadoc 中包含每个 function 的时间/空间复杂度?

[英]Why doesn't Java include the time/space complexity of each function in the javadoc?

Hi I want to know what is the time complexity of the "replaceAll" function of the String class but I can't find any information on it.( http://docs.oracle.com/javase/6/docs/api/java/lang/String.html )嗨,我想知道字符串 class 的“replaceAll”function 的时间复杂度是多少,但我找不到任何相关信息。( http://docs.oracle.com/javase/6/docs/api/ java/lang/String.html )

Wouldn't it be better for Java to include the complexities in the Javadoc? Java 将复杂性包含在 Javadoc 中不是更好吗? I believe it's a very important thing for someone to know.我相信有人知道这是一件非常重要的事情。

Most functions have fairly straight forward time complexities.大多数函数具有相当直接的时间复杂度。 AFAIK, replaceAll is O(n)据我所知,replaceAll 是 O(n)

IMHO.恕我直言。 Nothing beats testing this yourself empirically eg with a profiler, because its highly likely that 99% of the methods you use have little to no impact on the performance of your application.没有什么比自己凭经验测试它更好的了,例如使用分析器,因为很可能您使用的 99% 的方法对应用程序的性能几乎没有影响。

The complexity may be documented if guaranteed.如果有保证,可以记录复杂性。 For example, some of the collections classes document complexity guarantees.例如,一些 collections 类记录了复杂性保证。 For example, fromHashMap :例如,来自HashMap

This implementation provides constant-time performance for the basic operations (get and put)...此实现为基本操作(get 和 put)提供恒定时间性能......

However, sometimes the complexity is:但是,有时复杂性是:

  • Not guaranteed, and free to change with modifications to the implementation.不能保证,并且可以随着对实现的修改而自由更改。
  • Obviously O(1).显然是 O(1)。

The javadocs of the Java API specify a general contract of what must be done by each method, not how . Java API 的 javadoc 指定了每个方法必须做什么的一般契约,而不是如何做。 Each implementor of the API (say, OpenJDK, Oracle's JDK, etc.) has a certain freedom on how to implement each contract, and that freedom may include making optimizations, even sacrifices in performance. API 的每个实现者(比如 OpenJDK、Oracle 的 JDK 等)在如何实现每个合约方面都有一定的自由度,这种自由度可能包括进行优化,甚至牺牲性能。 So the javadocs in general don't specify details such as time/complexity of functions, unless it's absolutely necessary for a method to meet certain performance requirements.因此,javadocs 通常不会指定函数的时间/复杂性等细节,除非方法满足某些性能要求是绝对必要的。

If you're using the space / time complexity of basic operations to drive design decisions, then you're almost certainly doing it wrong.如果您使用基本操作的空间/时间复杂性来推动设计决策,那么您几乎可以肯定做错了。

First build a correct application, then profile it.首先构建一个正确的应用程序,然后对其进行概要分析。 Then optimize what the profiling process reveals as the bottlenecks.然后优化分析过程揭示的瓶颈。

The general answer is that the complexity typically depends on factors that are too difficult to analyse.一般的答案是,复杂性通常取决于难以分析的因素。 This certainly applies for String.replaceAll , where the effective complexity depends critically on the regex string.这当然适用于String.replaceAll ,其中有效复杂性主要取决于regex字符串。 (A poorly designed regex can make matching veerryyy slow.) (设计不当的正则表达式会使匹配 veerryyy 变慢。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM