简体   繁体   English

如何查看完成方法调用所花费的时间?

[英]How can I see the time taken to complete a method call?

I want to do some research to improve my programmer skills by seeing see how long a method takes to finish. 我想做些研究来提高我的程序员的技能,看看方法完成需要多长时间。

But I don't want to write any code in the java files I am testing because I have a lot of methods to test (for some of the code I do not have permission to edit the code) so if possible I just want to "watch" the methods. 但是,我不想在正在测试的Java文件中编写任何代码,因为我有很多测试方法(对于某些代码,我没有编辑代码的权限),因此,如果可能,我只想“观看”方法。

For example: 例如:

public void methodZero(){
  methodOne();
  methodTwo();
}

Should ideally print something like: 理想情况下应打印以下内容:

Time of methodZero = x. Time of methodOne = y. Time of methodTwo = z.

Question: Can I measure timing like this? 问题:我可以这样测量时间吗? Is there some additional info that would be important to me, like memory use? 是否有一些对我来说很重要的其他信息,例如内存使用情况?

Timing a method in isolation is usually completely meaningless, for a start you need statistical samples. 孤立地对方法进行计时通常是完全没有意义的,一开始您需要统计样本。 If you want to get run times for a method you have to take a lot of things into consideration and give as much context to the target of your timing as possible. 如果要获取方法的运行时间,则必须考虑很多因素,并为定时目标提供尽可能多的上下文。

For example, suppose your method has a return value, but you don't use it in any way in your benchmark - you are possibly going to encounter "dead-code elimination" by the JVM, which makes your timings meaningless. 例如,假设您的方法有一个返回值,但是您在基准测试中没有使用它-您可能会遇到JVM的“死代码消除”,这使您的计时毫无意义。 The JVM does many clever things like this ( openjdk.net: Performance techniques used in the Hotspot JVM ), all of which confuse and complicate taking meaningful timings. JVM会执行许多此类聪明的事情( openjdk.net:Hotspot JVM中使用的性能技术 ),所有这些事情都使有意义的时间变得混乱和复杂。

A good library to use to help you do this (that also has good documentation and tutorials) is JMH . JMH是一个很好的库来帮助您完成此操作(该库也有不错的文档和教程)。

Just as important as actual timings, but often related to taking meaningful timings, is measuring algorithm space and time complexity . 与实际计时同样重要,但通常与采取有意义的计时有关,它是测量算法空间和时间复杂度 This allows you to understand how your algorithm will grow as the input dataset changes in size. 这使您可以了解随着输入数据集大小的变化,算法将如何增长。 For example MethodA might be faster on a small input array and impractically slow on an input array 100x bigger, whereas MethodB may take the same time regardless of array size. 例如,在较小的输入数组上, MethodA可能更快,而在较大的输入数组上, MethodA可能不那么慢,而无论数组大小如何, MethodB可能花费相同的时间。

If you want to find out where in a program you should start looking to improve performance you can use a profiler ( eg eclipse.org: An introduction to profiling Java applications ). 如果要找出程序中的哪个位置应该开始寻求提高性能,可以使用探查器( 例如eclipse.org:Java应用程序性能分析简介 )。 This will help you identify things such as: high memory usage, high GC usage, total method time (eg low method call count but high execution time, or high call count and small but significant execution time). 这将帮助您识别诸如以下各项:高内存使用率,高GC使用率,方法总时间(例如,方法调用数少但执行时间长,或者调用计数高且执行时间短而重要)。 However profilers will impact on your program's execution time as well. 但是,探查器也会影响程序的执行时间。

TL;DR: profiling and performance testing is hard. TL; DR:分析和性能测试很困难。 Often you aren't really measuring what you think you are measuring. 通常,您并没有真正衡量自己认为要衡量的内容。

long startTime = System.nanoTime();
methodOne();
long endTime = System.nanoTime();
long duration = (endTime - startTime);

You as a programmer should be more interested in complexity (Usually Time Complexity, but sometimes you should also worry about Space Complexity) of your algorithm rather than actual time taken to execute the program. 作为程序员,您应该对算法的复杂性(通常是时间复杂性,但有时您还应该担心空间复杂性)感兴趣,而不是对执行程序所花费的实际时间感兴趣。 Have a read here about analysing algorithm complexity 在这里阅读有关分析算法复杂性的信息

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

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