1

I have a few java class files, each having a main method. Since these are compiled classes, I can not modify the code and track the system time before and after invoking the main method of the classes. Is there any way I can get this measure from the JVM or any other tool (preferably command-line)?

2 Answers2

0

Create a blank empty application doing nothing, time that starting up.

Time your test application.

Subtract the difference.

Be aware though that you are running into all sorts of issues with JIT, class loading times, etc which will impact on the accuracy of your measurements. At the very least you need to repeat the test a lot of times.

The measurement is also pretty meaningless by itself though. It may be better if you look at why you want to know this and from that see if there is a more useful question you can ask.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Thank you. The reason for doing this measurement is to empirically measure the impact of using some specific code fragments on the execution time of a program. But all I have are the .class files. Do you have any suggestion on a better way to approach the problem? – Samir Hasan Jan 08 '14 at 20:07
  • Load the class files as a library, write a benchmarking program to load that library and run speed tests against the actual calls you want to measure. Be aware of micro benchmarking pitfalls – Tim B Jan 08 '14 at 20:08
  • http://stackoverflow.com/questions/20655963/java-benchmarking-why-is-the-second-loop-faster/20655996#20655996 – Tim B Jan 08 '14 at 20:08
0

You could implement a class like this:

class caller {
        public static void main(String[] args) {
                long initialTime = System.currentTimeMillis();
                precompiled_class_name.main(args);
                long finalTime = System.currentTimeMillis();
                System.out.println("Spent time: " + (finalTime - initialTime));
        }
}

If you need more internal information, use a profiler like this one: http://jrat.sourceforge.net/

java -javaagent:shiftone-jrat.jar caller

That's command-line, but you'll need a desktop application to read the results.

KikoV
  • 899
  • 6
  • 13