命令行的快速基准测试

rgv*_*ley 7 php bash benchmarking

我想对PHP脚本进行基准测试,但这同样适用于可以从命令行运行的任何内容.

使用bash有一种简单的方法来对脚本进行基准测试,即多次运行命令并计算需要多长时间?

Xiè*_*léi 15

在命令行中,您可以:

$ time php foobar.php
Run Code Online (Sandbox Code Playgroud)

time是一个内置的bash.

对于多次运行:

$ time for a in {1..10}; do php foobar.php; done
real    0m13.042s
user    0m0.021s
sys     0m0.044s
Run Code Online (Sandbox Code Playgroud)

但是,您需要手动计算平均时间.

  • `perf stat -r10 php foobar.php` 将为您运行 10 次并平均 + stddev,至少对于“任务时钟”CPU 时间。(不一定是挂钟时间,如果您阻塞 I/O 或同时使用多个内核,挂钟时间可能会有所不同。) (2认同)