在Java中,我可以使用单个方法作为其他方法的计时器吗?
测试运行时时,我按以下方式使用 System.nanotime():
startTime = System.nanotime();
// some statements
System.out.println("Runtime: " + (System.nanoTime() - startTime));
有没有办法为程序中的其他代码块重用此测试模型?换句话说,我可以将其创建为方法并在测试期间将其他方法传递给它吗?
When testing runtime, I use System.nanotime() in the following way:
startTime = System.nanotime();
// some statements
System.out.println("Runtime: " + (System.nanoTime() - startTime));
Is there a way to reuse this test model for other blocks of code in my programs? In other words, can I create this as a method and pass other methods to it during testing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你可以,但它比你的方法更复杂:
在 Java 7 中,这会变得更简单(我省略了“Measure”实现):
顺便说一句,最好使用 System.nanoTime( ) 比
System.currentTimeMillis()
更准确,不仅因为前者更准确,还因为后者会在夏令时更改(或其他系统时间更改)时出现卡顿。Yes, you can, but it's even more complicated than your approach:
In Java 7, this will get a bit simpler (I left out the 'Measure' implementation):
By the way, it's better to use
System.nanoTime()
thanSystem.currentTimeMillis()
, not only because the former is more accurate, but also because the later will hickup around summertime change (or other system time changes).使用 AOP 可能是您最好的选择,它将允许您从外部定义要进行性能测试的方法,并自动用此计时代码来修饰这些方法,而无需修改源代码。
请参阅http://dhruba.name/2008/12/16/spring -aop-timing-aspect/
Using AOP is probably your best bet, it will allow you to externally define what methods you want to performance test, and automatically embellish those methods with this timing code, without having to modify the source code.
See http://dhruba.name/2008/12/16/spring-aop-timing-aspect/
分析器:
实际上我更喜欢通过探查器运行代码。我实际上对瓶颈感兴趣,我想这就是你所追求的。
确实存在其他替代方案。
JUnit 和 JPerf:
如果您有 junit 测试,那么使用 jperf 就是小菜一碟。
动态代理:
如果您的应用程序中有接口,并且您只对接口调用感兴趣,请创建一个动态代理并将其用于时间戳。
秒表对象:
比你的方法更简单的方法是创建一个 Stopwatch 对象(apache commons 有一个),具有开始、暂停、恢复、圈停止方法并使用它。这比您的代码效果更好,因为秒表实例可以与回调、事件和线程一起使用。
AOP:
除了分析器之外,几乎所有上述选项都可以使用自定义代码来实现,但如果您有足够的好奇心来学习 AOP,那就太好了。
Profiler:
I actually prefer running the code through the profiler. I am actually interested in the bottlenecks, and I assume that is what you are after.
Other alternatives do exist.
JUnit and JPerf:
If you have junit tests, then using jperf is a piece of cake.
Dynamic Proxy:
If you have interfaces in your application and iff you are only interested in the interface calls, create a dynamic proxy and use it for time stamping.
Stop watch object:
A simpler approach than yours is to create a Stopwatch object (apache commons has one), with start, pause, resume, lap stop methods and use it. This works better than your code as stop watch instances can be used with callbacks, events and threads.
AOP:
Other than profilers almost all the above options can be realized using custom code, but if you have are curious enough to learn AOP, it would be great.
三个附加答案。首先是(相对)明显的一个:
一个奇怪的,缺点是你会收到警告(变量 v 永远不会被读取)。我想知道这是否可以改进:
以及作弊:
Three additional answers. First the (relatively) obvious one:
A weird one, with the disadvantage that you get a warning (variable v is never read). I wonder if this could be improved:
And cheating: