Observe running time of a Java code in cmd prompt

我的未来我决定 提交于 2019-12-10 12:03:14

问题


I have a java class file in my hand and can simply able to run it from windows cmd to view the output. Only thing I need to observe the running time of this java program depending on different input parameters. I don't have the source code so I can not modify it to generate the running time for me. Please suggest possible way out to observe this. Thanks in advance.


回答1:


Wrap it in another class and run that. For example if your class (to which you dont have code) is called Runner then you may be currently running it as

java -jar jarContainingRunner.jar Runner

If you create another class that then calls Runner you can do anything in that class. For example you could call the class InstrumentedRunner.

public class InstrumentedRunner {

   public static void main(String... arg) {
       long start = System.currentTimeMillis();
       new Runner().run();
       long dur = System.currentTimeMillis() - start;
       System.out.format("Runner took %s milliseconds", dur);
   }
}

(I haven't tested the code above.)

You could then run

java -jar jarContainingRunner.jar;jarContainingInstrumentedRunner.jar instrumentedRunner

It would then run Runner's run method (assuming that is the method you want to time) and output the time it took.




回答2:


What you can do is you can write a batch file and there you can call your java class but before this save start time in variable and after class finish subtract it with start time.




回答3:


You can write a simple Java program to observe this. Use runtime executions within your new code and observe the execution time.

A sample overview of this new code:

//... before your class runs.
Runtime.getRuntime().exec("java -jar yourClass"); 
//... after your class runs.


来源:https://stackoverflow.com/questions/12367331/observe-running-time-of-a-java-code-in-cmd-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!