在cmd提示符下观察Java代码的运行时间 [英] Observe running time of a Java code in cmd prompt

查看:204
本文介绍了在cmd提示符下观察Java代码的运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手里有一个Java类文件,可以从Windows cmd运行它来查看输出.我只需要根据不同的输入参数来观察此Java程序的运行时间.我没有源代码,因此无法修改它来为我生成运行时间.请提出可能的观察方法.预先感谢.

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.

推荐答案

将其包装在另一个类中并运行它.例如,如果您的类(您没有代码)被称为Runner,那么您当前可能正在以

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

如果创建另一个类然后调用Runner,则可以在该类中执行任何操作.例如,您可以调用类InstrumentedRunner.

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

然后您可以运行

java -jar jarContainingRunner.jar;jarContainingInstrumentedRunner.jar instrumentedRunner

然后它将运行Runner的run方法(假设这是您要计时的方法)并输出花费的时间.

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

这篇关于在cmd提示符下观察Java代码的运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆