0

I have the following piece of code to try and determine how long a method call takes to execute:

long start = System.nanoTime();

diag(); // diag() is a native method, called via JNI.

long finish = System.nanoTime();

System.out.println("update() took: " + Long.toString(finish - start));

When being executed on a specific Android device (Xperia P), this diff (finish - start) is 0 most of the time.

What is the reason for this? and is there any better way to try measuring the time this method takes to execute and return?

*Note that other Android devices behave differently

4

1 回答 1

1

这是什么原因?

System.nanoTime()的分辨率取决于设备和操作系统。因此,如果您的设备的内部时钟分辨率太高,System.nanoTime()将无法改进。

有没有更好的方法来尝试测量此方法执行和返回所需的时间?

您可以多次运行该方法以增加花费的时间。但请注意,基准测试是一门复杂的艺术。您可以从这篇文章开始:如何在 Java 中编写正确的微基准测试?虽然它专注于热点 JVM,但大多数原则都适用于 Dalvik。

于 2013-07-23T10:42:49.877 回答