2

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.

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?

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
Lupiyo
  • 169
  • 1
  • 10
  • Simply log the starting time and ending time, then subtract and present? – Mena Aug 06 '15 at 15:40
  • you could either use a profile e.g jprofile, yourkit etc... and this will give you all the information that you need about CPU usage, memory cost, nb of calls etc.. or use something like the post below – unique_ptr Aug 06 '15 at 15:45
  • You say you want to improve your programming skills, and you want to do it by timing a bunch of routines? Let me suggest another approach. Write some serious code to accomplish some purpose you have in mind. Then do performance tuning on it, as in [*this example*](http://stackoverflow.com/a/927773/23771). – Mike Dunlavey Aug 06 '15 at 17:32

2 Answers2

4

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. 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.

A good library to use to help you do this (that also has good documentation and tutorials) is 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.

If you want to find out where in a program you should start looking to improve performance you can use a profiler (e.g. eclipse.org: An introduction to profiling Java applications). This will help you identify things such as: high memory usage, high GC usage, total method time (e.g. low method call count but high execution time, or high call count and small but significant execution time). However profilers will impact on your program's execution time as well.

TL;DR: profiling and performance testing is hard. Often you aren't really measuring what you think you are measuring.

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
  • So, when people comment that MethodA takes X seconds and MethodB takes Y seconds, it is irrelevant if i do not look as overall? Is Studying step by step is wrong and useless so? :( – Lupiyo Aug 06 '15 at 16:01
  • @Lupiyo. Unless they have done their benchmarking properly it is useless. If comparing like-with-like, using a good benchmark, it is fine. If the method operates on arrays or collections, then you also want to know the space and time complexity of each method. `MethodA` might be faster on a small test dataset and impractically slow on a dataset 100x bigger, `MethodB` may take the same time regardless of dataset size. – Andy Brown Aug 06 '15 at 16:04
  • This is exactly what I was looking for, I`m using now the JVM Monitor, a plugin. It shows every point that i want to cover. Thx! – Lupiyo Aug 06 '15 at 18:39
2
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

akalikin
  • 1,071
  • 12
  • 35
  • Thanks for the answer! However, I don't want to do code modifications (I`ll update my questions for this), because sometimes i have like 150 methods to see, and put 3 lines is like write 450 lines. I would like some watch or things likes these. But thanks, if I do not find anything better, ill do that. Thx – Lupiyo Aug 06 '15 at 15:53
  • @Lupiyo have a look here, seems to be exactly what you're looking for: http://www.ej-technologies.com/products/jprofiler/overview.html – akalikin Aug 06 '15 at 15:56