Hi all,

I have given a assignment to implement patterm matching algorithms and to
do a experimental analysis on number of comparisons, real time taken and CPU time taken for
each algorithm.
I have done all except CPU time calculation.

Plz can anyone guide me how to do this...

Would the System class's currentTimemillis method help?
I don't know how to ask the OS how much CPU time a program has used.

long time2=System.nanoTime();
timeBM=time2-time1;

Above gives me real-time time taken for the algorithems.
But i need to get the CPU time.(I dont knw to whether CPU usage.But lecture has given CPU time)
Do CPU time, CPU usage and real_time mean the same thing?

I don't think you have access to the actual CPU time, but if your program has no I/O, and there's nothing else running on the machine at the same time, then for most purposes you can assume CPU time == real time.
(ie, roughly, real time = CPU time + I/O time + time taken by other processes)

if your program has no I/O, and there's nothing else running on the machine at the same time, then for most purposes you can assume CPU time == real time

If the program in multi-threaded, the CPU time gives the "total" time spent by each CPU/core. So even if the real/wall time is 1s, it's quite possible that the CPU time might be 4s for a multi-threaded CPU intensive code.

@corol
There are two techniques which you can put to use:

  1. Use the JMX API to get an approximate CPU time for all threads
  2. Use the time unix command when executing your Java process

For (1), you can have a look at the Thread MBean. Here is a small snippet:

public class TestIt {

    public static void main(String[] args) throws Exception {
        int numThreads = 5;
        long start = System.currentTimeMillis();
        for (int i = 0; i < numThreads; ++i) {
            new MyThread().start();
        }
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        long[] allThreadIds = threadMXBean.getAllThreadIds();
        System.out.println("Total JVM Thread count: " + allThreadIds.length);
        long nano = 0;
        for (long id : allThreadIds) {
            nano += threadMXBean.getThreadCpuTime(id);
        }
        System.out.printf("Total cpu time: %s ms; real time: %s", nano / 1E6, (System.currentTimeMillis() - start));
    }

}

class MyThread extends Thread {

    public void run() {
        int sum = 0;
        for (int i = 0; i < 1000000; ++i) {
            sum += i;
        }
        sum = sum + 1;
    }

}

Of course, this snippet has problems of its own. This code won't consider threads which have died out. If you have a code which doesn't use explicit threads, this snippet should work out fine for you. If you are using threads, make sure you are using an Executor which has a fixed pool of threads to get a correct reading.

For (2), just execute the Java program with the time unix command to get real/cpu timings. Something like:

time java -cp .:some.jar:other.jar my.main.class.MainClass

Thanks It works.
Plz explain this line. What is IE6?
System.out.printf("Total cpu time: %s ms; real time: %s", nano / 1E6, (System.currentTimeMillis() - start));

Now it gives cpu time.
Is the given cpu time included in real time?

What i want to know is that, Has 62500ns spent out of 6561169ns of total real time?

The figures are in nano seconds and what i got when executing.
Following code is the code that i executed.

I edited your code a bit coz my pattern matching code does not have threds. It is just a simple bunch of lines.

  class test {
    public static void main(String[] args) throws Exception {
    long start = System.nanoTime();//System.currentTimeMillis();

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    long[] allThreadIds = threadMXBean.getAllThreadIds();
    System.out.println("Total JVM Thread count: " + allThreadIds.length);
    long nano = 0;
    for (long id : allThreadIds) {
        nano += threadMXBean.getThreadCpuTime(id);
    }
    System.out.printf("Total cpu time: %s ms; real time: %s", nano / 1E6,     (/*System.currentTimeMillis()*/System.nanoTime() - start));
    }
} 

Plz explain this line. What is IE6?

That means 1 * 10^6 i.e. 1 followed by 6 zeroes. I use this number to convert from nanoseconds (10^-9) to milliseconds (10^-3).

Is the given cpu time included in real time?

These are two different things. The "real time" is the "wall clock" time. This means that you run your Java code, start a stopwatch and stop it when the program completes. The time you get is the "real time" taken by your code to execute. Even if you don't explicitly create threads, the JVM has its own set of threads: the main thread which executes your code, the GC thread etc. If your code is doing a lot of IO and waiting around for stuff, it's quite possible that the "CPU" time might be quite lower than the real time but if you are doing extensive computations on a multi-core box, the figures might be reversed.

You can always verify your timings by running the same program using the *nix time command as already mentioned in my previous post but it will give you the time in millisecond resolution and not nanosecond.

Thank you very much for saving me. I'll use ur code inside my
program.

Once again thank you....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.