平均执行时间

发布于 2024-09-24 09:03:08 字数 129 浏览 8 评论 0原文

有没有什么好的 GNU 方法来测量某些命令行程序的平均(最坏情况,最好情况)执行时间?我有图像过滤器,未指定数量的图片,使用 bash 中的 for 循环过滤它们。到目前为止,我正在使用时间,但我找不到获取一些统计数据的方法。

is there any nice GNU way how to measure average (worst case, best case) execution time of some command line program? I have image filter, unspecified amount of pictures, filtering them using for-loop in bash. So far I am using time, but I can't find a way how to get some statistics.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪我闹别瞎闹 2024-10-01 09:03:08

您可以将时间输出发送到某个文件,然后“工作”该文件

echo "some info" >> timefile.txt
time ( ./yourprog parm1 parm2 ) 2>> timefile.txt

You can send the output of time to some file, and then "work" that file

echo "some info" >> timefile.txt
time ( ./yourprog parm1 parm2 ) 2>> timefile.txt
笨笨の傻瓜 2024-10-01 09:03:08

有一个有趣的 Perl 程序,名为 dumbbench,它本质上是时间的包装器命令。它会多次运行您的程序,丢弃异常值,然后计算一些统计数据。

作者有几篇文章(此处此处)概述a)为什么基准测试很糟糕,b)你可以制作什么样的漂亮图表来让你的基准测试数据不那么糟糕。

There's an interesting Perl program called dumbbench that's essentially a wrapper around the time command. It runs your program a number of times, throws away outliers, then calculates some statistics.

The author has a couple of articles (here and here) outlining a) why benchmarking sucks, and b) what kind of pretty graphs you can make to make your benchmarking numbers suck a little less.

泪意 2024-10-01 09:03:08

您的时间走在正确的轨道上。我用它来执行小型代码执行分析。

然后,我使用 python 通过读取 time 的输出来收集统计信息。为了提高准确性,我通常会尝试 10 - 1000 次,具体取决于每个过程需要多长时间。

我不熟悉任何进行此类分析的预装 GNU 应用程序。

You're on the right track with time. It's what I use to preform small code execution analyses.

I then use python to collect the statistics by reading the output of time. In order to increase accuracy, I typically do the trial 10 - 1000 times, depending on how long each process takes.

I'm not familiar with any pre-installed GNU application that does this sort of analysis.

养猫人 2024-10-01 09:03:08
#!/bin/bash
for i in {1..100}
do
  env time --append -o time_output.txt   ./test_program --arguments-to-test-program
done
exit

如果您发现 {1..100} 语法不适合您,那么您应该查看 seq 命令。

我使用 env time 来执行时间程序,而不是 shell 的内置命令,后者不采用时间程序采用的所有参数。时间程序还采用其他参数来改变其输出的格式,您可能希望使用它来使数据更容易被另一个程序处理。 -p (--portability) 参数使其以 POSIX 格式输出(就像 BASH 的内置时间一样),但使用 -f 选项您可以获得更多控制。 man 1 次 了解更多信息。

收集数据后,一个简单的 Perl 或 Python 脚本就可以轻松解析和分析您的计时数据。

#!/bin/bash
for i in {1..100}
do
  env time --append -o time_output.txt   ./test_program --arguments-to-test-program
done
exit

If you find that the {1..100} syntax doesn't work for you then you should have a look at the seq command.

I used the env time to execute the time program rather than the shell's built in command, which does not take all of the arguments that the time program takes. The time program also takes other arguments to alter the format of it's output, which you will probably want to use to make the data easier to process by another program. The -p (--portability) argument makes it output in the POSIX format (like BASH's builtin time does), but using the -f option you can get more control. man 1 time for more info.

After you have gathered your data a simple perl or python script can easily parse and analyze your timing data.

捎一片雪花 2024-10-01 09:03:08

您应该考虑是否对外部循环进行计时并除以重复次数,而不是单独对每个迭代进行计时。如果您担心丢弃高点和低点,只需再进行几次迭代即可将它们淹没。

time for i in {1..1000}
do
    something
done

您可以捕获变量中时间的输出

foo=$( { time {
    echo "stdout test message demo"
    for i in {1..30}
    do
        something
    done
    echo "stderr test message demo" >&2
} 1>&3 2>&4; } 2>&1 )

并做一些假数学< /em>:

foo=${foo/.}          # "divide" by ...
echo "0.00${foo/#0}"  # ... 1000

或者只使用bc

echo "scale=8; $foo/1000" | bc

You should consider whether to time the outer loop and divide by the repetitions rather than timing each iteration separately. If you're worried about discarding the high and low, just do a few more iterations to drown them out.

time for i in {1..1000}
do
    something
done

You can capture the output from time in a variable:

foo=$( { time {
    echo "stdout test message demo"
    for i in {1..30}
    do
        something
    done
    echo "stderr test message demo" >&2
} 1>&3 2>&4; } 2>&1 )

and do some fake math:

foo=${foo/.}          # "divide" by ...
echo "0.00${foo/#0}"  # ... 1000

Or just use bc:

echo "scale=8; $foo/1000" | bc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文