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)?
Asked
Active
Viewed 1,289 times
1

Samir Hasan
- 13
- 4
-
1Not sure what level of accuracy you need, but linux has the time command built in http://www.thegeekstuff.com/2012/01/time-command-examples/ – niiru Jan 08 '14 at 19:38
-
Thank you, that shall give me a start. But doesn't that time also include the time taken for the JVM itself to load before the program runs? It would be great to be able to track the time JVM took to execute the program. – Samir Hasan Jan 08 '14 at 19:42
-
It probably wouldn't be from the command line, but you could use a `profiler`. – Elliott Frisch Jan 08 '14 at 19:47
-
Yes it will. Unfortunately that's the only tool I'm aware of! Hopefully someone else can give you a better answer... – niiru Jan 08 '14 at 19:47
-
use the bash command time See this question – Saad Attieh Jan 08 '14 at 19:52
-
You can use the bash command time see this SO question – Saad Attieh Jan 08 '14 at 19:53
-
"Since these are compiled classes" - what do you mean exactly? Compiled into .class files, like all Java classes eventually become? Or dynamically compiled by Hotspot? – Amir Afghani Jan 08 '14 at 20:03
-
Execute it in a loop one million times, and stopwatch it. The number of seconds translates to microseconds. That's what the old-timers do, and it always works. – Mike Dunlavey Jan 08 '14 at 20:19
2 Answers
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