0

I have written a java program that can compile, execute others submitted code(cpp, java). I am doing this using java's process class. Process class invoke the g++ or java compiler. For some purpose I need to know what is the actual execution time of that process or how many time that code takes to execute.

process.waitFor(2000, TimeUnit.MILLISECONDS)

This process is waiting for 2000 milisecond but If the process done in less than 2000 milisecond how can I know what is the time that process takes?

======Update===== I currently doing this like a old way.

StopWatch.Start();
process.waitFor(dto.getTimeLimit(), TimeUnit.MILLISECONDS)
long timeEplased = StopWatch.Stop();

StopWatch is my written class that calculate the elapsed time using System.nanoTime().

seal
  • 1,122
  • 5
  • 19
  • 37
  • You can do it [like this](http://stackoverflow.com/questions/8255738/is-there-a-stopwatch-in-java) or use existing tools like [this one](https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Stopwatch.html) – Nir Alfasi Aug 29 '16 at 21:11
  • @alfasin really ? I am currently doing this. Is there any smarter way ? I going to update my question that what I am doing right now. – seal Aug 29 '16 at 21:14
  • "really?" - yes. This is a simple task, by making it "smarter" you'll most surely end with over-complicated implementation. Keep it simple bud. – Nir Alfasi Aug 29 '16 at 21:38
  • @alfasin I am looking for a method that gives precision. :) there has to be some overhead using Process class. – seal Aug 29 '16 at 22:27
  • The method you're using is precise as well as accurate. – Nir Alfasi Aug 29 '16 at 22:30
  • @alfasin what about the overhead of Process class ? – seal Aug 29 '16 at 22:36
  • and what about the overhead of Java ? C++ ? why not program in assembly ? :) – Nir Alfasi Aug 29 '16 at 22:42
  • 1
    he he .. good point . btw thanks. :) – seal Aug 29 '16 at 22:53

1 Answers1

0

It should be sufficient to do it like that:

    long before = System.nanoTime();

    process.waitFor();

    long durationMs = (System.nanoTime() - before)/1_000_000;

Provided your thread is not interrupted and the task halts. You can start that in a new thread and interrupt it when your time limit is exceeded, too.

I don't think there is a dedicated interface for specifically that task, unfortunately - probably because the concept of the process running time can very between different OSes (are you interested in the processor time, userspace time, kernelspace time, monotonous clock time? These all would have to be handled, and that's just a generix *NIX)

mszymborski
  • 1,615
  • 1
  • 20
  • 28
  • I am currently doing this. I thought there is some other way. :/ – seal Aug 29 '16 at 21:15
  • 2
    @seal - I don't think there is an interface for specifically that task, unfortunately. Probably because the concept of process running time can very between different OSes (are you interested in the processor time, userspace time, kernelspace time, monotonous time? These all would have to be handled, and that's just a generix *NIX) – mszymborski Aug 29 '16 at 21:19
  • I think you should add your previous comment to your answer. :) – seal Aug 29 '16 at 22:51
  • @seal: I've added it to the answer. – mszymborski Aug 29 '16 at 23:07