Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash:计算脚本运行所需的时间_Bash - Fatal编程技术网

Bash:计算脚本运行所需的时间

Bash:计算脚本运行所需的时间,bash,Bash,有没有一种方法可以让bash脚本打印它运行了多长时间 一些相当简单的事情,比如: #!/bin/bash something to start time command to be run something to calculate runtime and print result 您可以使用以下命令: 计时运行程序并总结系统资源使用情况 例如: 时间 将为您提供命令的执行时间有标准工具 剧本: #!/bin/bash echo start sleep 2 echo end 用法: $

有没有一种方法可以让bash脚本打印它运行了多长时间

一些相当简单的事情,比如:

#!/bin/bash
something to start time
command to be run
something to calculate runtime and print result
您可以使用以下命令:

计时运行程序并总结系统资源使用情况

例如:

时间


将为您提供命令的执行时间

有标准工具

剧本:

#!/bin/bash

echo start
sleep 2
echo end
用法:

$ time ./so.sh       
start
end
./so.sh  0.00s user 0.01s system 0% cpu 2.007 total

正如其他答案中所写,时间是整个剧本的最佳时间

另一方面,如果只想计算部分脚本的长度,请尝试使用以下代码:

#!/bin/bash
T="$(date +%s)"

sleep 2

T="$(($(date +%s)-T))"
echo "It took ${T} seconds!"
这只是需要一些约会

自1970-01-01 00:00:00 UTC以来的秒数(有关更多信息,请参阅人员日期)

一个在执行前,一个在执行后,从另一个中减去一个


您可以尝试一下,以获得更好的准确性。

感谢您的快速响应。感谢您提供的一般信息。