How to measure the CPU time a program takes?

W

www

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

Gordon Beaton

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

--
 
W

www

Gordon said:
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.
 
G

Gordon Beaton

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

--
 
S

Stefan Ram

www said:
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/
 
W

www

Gordon said:
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.)
 
W

www

Gordon said:
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.
 
G

Gordon Beaton

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

--
 
P

Patricia Shanahan

www said:
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.

Have you looked at java.lang.management, especially ThreadMXBean?

Patricia
 
S

Sherm Pendley

www said:
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.

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

Lew

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

www

Patricia said:
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.
 
L

Lew

www said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top