java cmd 执行时间,在cmd提示符下观察Java代码的运行时间

本文介绍如何在没有源代码的情况下,通过创建一个包裹类InstrumentedRunner来测量Java程序的运行时间,使用System.currentTimeMillis()记录前后时间差。通过运行包含InstrumentedRunner的jar文件,实现对指定程序运行时间的观测。
摘要由CSDN通过智能技术生成

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值