java中的命令执行时间

w7t8yxp5  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(389)

这个问题在这里已经有答案了

在java中继续之前等待进程完成(4个答案)
如何在java中测量所用的时间[重复](15个答案)
上个月关门了。
有没有办法知道命令的执行时间?制作 exec 同步

Runtime rt = Runtime.getRuntime();
Process process = rt.exec(ffmpeg +" -i "+source+" -vcodec h264 -acodec aac  "+destination);

ffmpeg命令需要一些时间,所以我想在执行之后记录一些内容。有没有办法在命令执行后记录这些事情?

e0bqpujr

e0bqpujr1#

您可以记录进程开始前的时间,等待它完成,然后再次记录时间,并减去这两个时间:

long before = System.currentTimeMillis();
Runtime rt = Runtime.getRuntime();
Process process =
    rt.exec(ffmpeg + " -i " + source + " -vcodec h264 -acodec aac  " + destination);
process.waitFor();
long after = System.currentTimeMillis();
long execTime = after - before;
System.out.println("Processing took " + execTime + " milliseconds");

相关问题