Question

I'm programming a little Javagame for Android and would like to know if there is a possibility in Eclipse to find out which method takes the most time to execute.

Was it helpful?

Solution

It depends what're you going to test.

In case you develop applications for Android you should try out the TimingLogger class. Take a look at this article describing the usage of the TimingLogger helper class.

A very goot tool is JMeter and there is a plugin for Android as well.

if you don't want to use external tools, but a very standard way, to measure elapsed time, you must use System.nanoTime(). You shouldn't use currentTimeMillis, because it measures wall-clock time ans, as no computer's clock is perfect (them all occasionally needs to be corrected) there's a process that runs and continually issues small corrections to the system clock. Not to mention the leap second correction.

Although currentTimeMillis is often used, it's still incorrect to measure elapsed time and timing. Anyhow, as the invocation takes some time, you should not expect to correctly time very very small intervals. But that shouldn't be an issue working with Android.

I'll show you an example:

long startTime = System.nanoTime();

// run/call the method

long endTime = System.nanoTime();
long diff = lEndTime - lStartTime;
System.out.println("Elapsed milliseconds: " + difference/1000000);

You could have a look at this free library as well: http://jetm.void.fm/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top