Java Forum / General / April 2007
How to measure the CPU time a program takes?
www - 04 Apr 2007 19:06 GMT Hi,
I have a Java program. I am running it on RedHat Linux. I want to measure how long it takes to run it. One way is to set a timer right at the beginning. At the end measure how long the time has passed. But this strongly depends on how busy the CPU is, since there are many users use it. e.g. In the weekend, when there are few users, my program will run extremely fast. I hope to find out how much CPU computation time it takes.
I am using Eclipse. I know profiling plug in a little bit. I am not sure it can give me what I want.
Could you provide me some more information or tips?
Thank you very much.
Gordon Beaton - 04 Apr 2007 19:18 GMT > I have a Java program. I am running it on RedHat Linux. I want to > measure how long it takes to run it. [...]
> Could you provide me some more information or tips? Yes, don't reinvent the wheel:
foo$ time java MyClass
/gordon
--
www - 04 Apr 2007 19:35 GMT > Yes, don't reinvent the wheel: > > foo$ time java MyClass > > /gordon Thank you. It is simple and great.
Now, I have one more question. If I don't want to measure the whole program time usage and I just want to measure a specific block of code time usage, how can I do it?
Thank you.
Gordon Beaton - 04 Apr 2007 19:44 GMT > Now, I have one more question. If I don't want to measure the whole > program time usage and I just want to measure a specific block of > code time usage, how can I do it? Call System.currentTimeMillis() before and after the block, and subtract. If once through the block isn't long enough to get an accurate measurement, put the block inside a loop and measure the loop time instead, then divide the result accordingly.
/gordon
--
www - 04 Apr 2007 19:53 GMT > Call System.currentTimeMillis() before and after the block, and > subtract. If once through the block isn't long enough to get an > accurate measurement, put the block inside a loop and measure the loop > time instead, then divide the result accordingly. > > /gordon This will give me the "apparent" time usage, not the CPU time that block of code consumed. I am more interested in finding out the consumed CPU time, instead of user time.
Gordon Beaton - 04 Apr 2007 19:59 GMT > This will give me the "apparent" time usage, not the CPU time that > block of code consumed. I am more interested in finding out the > consumed CPU time, instead of user time. In that case you need to use native code to call getrusage() before and after the block.
/gordon
--
Patricia Shanahan - 04 Apr 2007 20:29 GMT >> Call System.currentTimeMillis() before and after the block, and >> subtract. If once through the block isn't long enough to get an [quoted text clipped - 6 lines] > of code consumed. I am more interested in finding out the consumed CPU > time, instead of user time. Have you looked at java.lang.management, especially ThreadMXBean?
Patricia
www - 05 Apr 2007 16:06 GMT > Have you looked at java.lang.management, especially ThreadMXBean? > > Patricia Thank you. I have looked it up and tried the following:
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long beginCPUTime = threadBean.getCurrentThreadCpuTime(); //in units of nanosecond
for-loop //my heavy duty computation
long endCPUTime = threadBean.getCurrentThreadCpuTime(); //nanosecond System.out.println("CPU Time used (millis): " + (endCPUTime-beginCPUTime)/1000000);
The print out value is very close to the one I obtained from the difference of System.currentMillis() after and before the loop. So it looks like giving me the "apparent" time user has spent for this block code, not CPU time.
I really like:
time java MyComputation
It gives the user time and system time spent for the whole program from beginning to the end.
I came a "silly" idea for measuring the system time of the for-loop by using "time" command:
for(int i=1; i<=20; i++) { for-loop //my heavy duty computation }
Then
time java MyComputation
The extra system time is due to 19 runs of for-loop. Divided it by 19 gives me the system time for a single for-loop.
I am going to use this to report to my boss.
Lew - 05 Apr 2007 22:28 GMT > I came a "silly" idea for measuring the system time of the for-loop by > using "time" command: [quoted text clipped - 10 lines] > The extra system time is due to 19 runs of for-loop. Divided it by 19 > gives me the system time for a single for-loop. No, it doesn't. It gives you the time for a single for loop plus 1/19th the time to start up the JVM, load all the classes and enter your program.
1/19th of that overhead could even be more time than all 19 loops combined, depending on how "heavy duty" the computation is.
 Signature Lew
Sherm Pendley - 04 Apr 2007 23:07 GMT >> Call System.currentTimeMillis() before and after the block, and >> subtract. If once through the block isn't long enough to get an [quoted text clipped - 4 lines] > block of code consumed. I am more interested in finding out the > consumed CPU time, instead of user time. What you're looking for is called a "profiler". The absolute numbers you get from a profiler aren't very meaningful - as another responder pointed out, they vary wildly as a result of runtime arguments, dynamic compilation, etc.
But the *relative* numbers you get from a profiler can pinpoint bottlenecks in your code. It can show you, for example, that 80% of the total CPU time used by your app was spent in a particular method; then you know focusing your optimization efforts on that method will give the biggest improvement.
On the other hand, if a method is highly inefficient but seldom called, it may use only 2% of the total CPU time - optimizing that method won't do a whole lot to improve the overall performance of your app.
Knowing the correct term can make all the difference. Googling for "java profiler" turned up this site, which lists a number of them, all open source:
<http://java-source.net/open-source/profilers>
There's also a language-neutral page on WikiPedia that discusses the use of profilers, and lists a number of them for various languages:
<http://en.wikipedia.org/wiki/Performance_analysis>
sherm--
 Signature Web Hosting by West Virginians, for West Virginians: http://wv-www.net Cocoa programming in Perl: http://camelbones.sourceforge.net
Stefan Ram - 04 Apr 2007 23:33 GMT >Knowing the correct term can make all the difference. Googling for "java >profiler" turned up this site, which lists a number of them, all open source: This reminded me of the fact that the JDK already contains a profiler.
Search for »-Xprof« within the page
http://download.java.net/jdk7/docs/technotes/tools/windows/java.html
Stefan Ram - 04 Apr 2007 19:48 GMT >Now, I have one more question. If I don't want to measure the whole >program time usage and I just want to measure a specific block of code >time usage, how can I do it? This is also known as »microbenchmarking«.
It is very complicated to do properly due to the dynamic code execution in Java, dependency on command line options, execution paths and so on.
That being said, one albeit imperfect approach is shown on:
http://www.purl.org/stefan_ram/pub/timethese-in-java
See also
http://www.mathcs.emory.edu/~dawidk/microbe/
www - 04 Apr 2007 19:49 GMT > Yes, don't reinvent the wheel: > > foo$ time java MyClass > > /gordon By the way, I repeated several times. I found that even System usage time varies each time. I can understand why each time user usage time is different, because it depends on whether CPU is busy or not. But why are System usage time different? (I assume it equals to CPU time.)
Lew - 04 Apr 2007 23:52 GMT Gordon Beaton wrote:
>> Yes, don't reinvent the wheel: >> >> foo$ time java MyClass
> By the way, I repeated several times. I found that even System usage > time varies each time. I can understand why each time user usage time is > different, because it depends on whether CPU is busy or not. But why are > System usage time different? (I assume it equals to CPU time.) It might be that the JVM startup time varies, according to, say, how much of it is in cache at invocation. When you time from the command line you time the whole sheband - JVM startup, class loading, and the code of interest.
 Signature Lew
Roedy Green - 04 Apr 2007 19:30 GMT >I have a Java program. I am running it on RedHat Linux. I want to >measure how long it takes to run it. see http://mindprod.com/jgloss/benchmark.html
 Signature Canadian Mind Products, Roedy Green, http://mindprod.com Priorities: Prevent global climate destabilisation. End both wars. Prepare for oil shortages.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|