10

I want to test the running time of certain code block as following:

start = System.currentTimeMillis();
{
   ...
   this.dosomething();
}
end = System.currentTimeMillis();
System.out.println(end - start);

when I optimize the code block, How can I annotate those codes which compute the time quickly like the following?

//start = System.currentTimeMillis();
{
   ...
   this.dosomething();
}
//end = System.currentTimeMillis();
//System.out.println(end - start);
Xing Shi
  • 2,152
  • 3
  • 21
  • 32
  • Something similar to [this answer](http://stackoverflow.com/a/4836219/477878)? – Joachim Isaksson Oct 28 '12 at 07:35
  • Not an answer to your question, but **please** don't use `System.currentTimeMillis()` for benchmarking purposes, use `System.nanoTime()` instead. And please read the answers to this question: [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Daniel Pryden Oct 29 '12 at 00:49

6 Answers6

4

Just use a profiler. It will instrument your code automatically with minimal footprint, displaying various statistics and hot spots. Check out profiler built into JVisualVM, JProfiler or YourKit.

When you don't need it, you don't pay anything for it.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • How do you `test the running time of certain code block` with a profiler (if the code block is neither a single operation nor a complete method)? – DaveFar Oct 29 '12 at 07:25
  • @DaveBall: indeed it's not possible. I guess the simplest way to work around it would be to simply extract the method (which is often a good idea on its own) – Tomasz Nurkiewicz Oct 29 '12 at 07:37
2

You can use annotation processing to create compile time annotations and generate source code based on that.

This article discusses about how to generate code Properties via Annotation Processing using annotations. You can define custom annotations like @Start, @End, @Calculate and use them like below. If you don't generate source code they will be anyways get removed.

@Start
{
      ...
      this.dosomething();
}
@End
@Caclulate

Note: It can not modify code being inspected so you will need to create subclass.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
1

If you just want something simple, you could always have a debug (boolean) variable that is true when you want debugging information to be printed, for example:

if (debug) start = System.currentTimeMillis();
{
   ...
   this.dosomething();
}
if (debug) {
    end = System.currentTimeMillis();
    System.out.println(end - start);
}

When I'm doing this, I often set debug using command line arguments so no code has to be changed to toggle debug mode.

EDIT: If you might want to repeat this structure elsewhere in the program, I would definitely go with AOP as mentioned in the other answers.

chm
  • 1,519
  • 13
  • 21
1

By separating the code for performance measurement from the main code from the beginning. Do one of the following:

  • as chm052 described in his anwswer, with some flag and guarded commands;
  • by using a logging framework with these guarded commands, e.g. SLF4J;
  • by using a (micro-)benchmarking tool, which helps you with good time measurements, statistics and the guarded commands (e.g. via AspectOrientedProgramming). You should definitely use such a tool to get sensible measurements (see e.g. https://stackoverflow.com/a/7120803/750378).
Community
  • 1
  • 1
DaveFar
  • 7,078
  • 4
  • 50
  • 90
1

The approach proposed by chm052 will certainly work, but it will couple the time measurement time logic to your business method, which is not desirable. After all the method is written to execute "dosomething()" - and before and after these you want to measure times. Tomorrow you'll want to log the statements link "entering the methods/leaving the method doSomething", in a while you'll probably want to check permissions on whether this code can be run at all.

I hope you've got my point, while this approach is good for simple projects it will pollute your code with unrelated concerns

There are a couple of techniques.

  1. AOP, this was already suggested by Joachim Isaksson. AOP really shines at this area. Create an advice where you'll use your timemeasurement logic and you're ready to go.

  2. If you want/need to stick to OOP technologies, I suggest you to create a Proxy or Decorator

Technically its really close to each other, its only how you bear in mind the intention of your concern.

Hope this helps

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
-1

I am not very clear of your meaning. if you just want to comment the code, then the short cut is ctrl+/ in eclipse. And if you try to kind this kind of code in you project, you can use log4j to do this kind of thing.

OQJF
  • 1,350
  • 9
  • 12
  • 2
    The original poster has not indicated which IDE they are using (Eclipse is just one of the many possibilities). Also, you will need to expand your answer to explain why log4j could help, because I can't see how. – Duncan Jones Oct 28 '12 at 08:04
  • I just joined this great place, so not know what the rule is and I am glad to share my idea. But if it's not allowed, then I feel sorry about it. – OQJF Oct 28 '12 at 11:31
  • StackOverflow would not exist without people sharing their ideas - that's fine. But it also thrives on peer review and constructive criticism. Not every answer you give will be correct and it's important to not be hurt if others question your answer. Not all comments are correct either, by the way :-) – Duncan Jones Oct 28 '12 at 13:30