1

I have a simple class and I would like to measure method call times how can I do that? Looking for generic way to achieve that so I can apply it to more difficult classes as well. Thank you

import java.io.*;
import java.util.*; 

public class Turtle {
  private String name;
  private Formatter f;
  public Turtle(String name, Formatter f) {
    this.name = name;
    this.f = f;
  }

  public void move(int x, int y) {
    f.format("%s The Turtle is at (%d,%d)\n", name, x, y);
  }

  public static void main(String[] args) {
    PrintStream outAlias = System.err;
    Turtle tommy = new Turtle("Tommy",
      new Formatter(System.out));
    Turtle terry = new Turtle("Terry",
      new Formatter(outAlias));
    tommy.move(0,0);
    terry.move(4,8);
    tommy.move(3,4);
    terry.move(2,5);
    tommy.move(3,3);
    terry.move(3,3);
  }
}
aretai
  • 1,619
  • 6
  • 19
  • 30
  • What mean you, "method call times"? – Hovercraft Full Of Eels Feb 25 '12 at 13:22
  • As one of the ppl suggested it was CPUT time. I wondered whether there is any Java built-in method. Not looking for anything complex such as profiler but rather something I can use ad hoc in my code. Something like this: long startTime = System.currentTimeMillis(); tommy.move(0,0); long endTime = System.currentTimeMillis(); System.out.println("That took " + (endTime - startTime) + " milliseconds");//but is there a way to do it in one line with some Java method? – aretai Feb 25 '12 at 14:19
  • @aretal Your example measures elapsed (wall) time, not CPU time. Apparently it's hard to this in one line as you need to at least mark the lines where the clock is supposed to start and stop somehow. Aspects can give you a compact solution, but it's probably overkill to dig into AOP if all you want is to measure time. – Michał Kosmulski Feb 26 '12 at 17:57
  • yes I understand the limitations of my solution, that's why hoped for a better one. It seems it is AOP. thank you anyway – aretai Feb 27 '12 at 00:00

4 Answers4

3

Use either a profiler or count them manually:

public static int MOVE_CALL_COUNT;


public void move(int, int)
{
    MOVE_CALL_COUNT++;
}

Profiling is recommended. In NetBeans there is a built-in profiler. Otherwise, VisualVM is a recommended option, which is the same profiler as in NetBeans.

If you really want to know how long it takes to run these methods, use a profiler.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • I think aretai means the amount of user or CPU time, as the post is tagged [time]. Also he directly asked for a generic methods, so the manual count is probably not a correct answer either. Of course, the use of a profiler *is* likely the way to proceed. – Maarten Bodewes Feb 25 '12 at 13:26
2

Try the profiler in the JDK, visualvm.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1

If it is testing to see how long each method takes to execute, then there are already some good questions relating to this on SO. Check out this one for example.

My preference, especially in a larger project with many methods, would be to use AOP so it doesn't involve changing existing code too much. This answer on the question I linked to above suggests AOP for the same reasons. As he suggests, going in depth in AOP is beyond the scope of this, but it is certainly worth a look. Take a look at this tutorial into how to use spring for method timings.

Community
  • 1
  • 1
Mr Moose
  • 5,946
  • 7
  • 34
  • 69
1

Use dedicated tools that will show You even more You need :) VisualVM is best one in my opinion, However there are other available (e.g. JConsole that is provided in JDK natively, or JRockit that is paid).
Read more here on my post

These tools shows running apps and resources consumed by them along with servlets (if any) processing times, error and call counts etc. Also threads and classes instantiated are listed. This will surely satisfy Your needs

Jacek Milewski
  • 3,304
  • 1
  • 18
  • 17