gpt4 book ai didi

ruby - 使用 Ruby Benchmark 时防止输出

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:28 26 4
gpt4 key购买 nike

我正在尝试比较 touch Rake 包含的用于对系统触摸进行脱壳的方法。

每个操作都将输出发送到标准输出:

require 'benchmark'
require 'rake'

n = 3
result = Benchmark.bm do |x|
x.report("sh:") do
n.times { sh "touch foo.txt" }
end
x.report("touch:") do
n.times { touch "bar.txt" }
end
end

结果:

user     system      total        real
sh:touch foo.txt
touch foo.txt
touch foo.txt
0.000000 0.010000 0.030000 ( 0.024775)
touch:touch bar.txt
touch bar.txt
touch bar.txt
0.000000 0.000000 0.000000 ( 0.000412)

我想要的是:

user     system      total        real
sh:touch foo.txt
0.000000 0.010000 0.030000 ( 0.024775)
touch:touch bar.txt
0.000000 0.000000 0.000000 ( 0.000412)

或者其他只有结果的东西。

我读入了another question使用 Benchmark.measure 但以下也不起作用:

require 'benchmark'
require 'rake'

n = 3
result = Benchmark.measure do
n.times { sh "touch foo.txt" }
end

puts result

结果:

touch foo.txt
touch foo.txt
touch foo.txt
0.000000 0.000000 0.010000 ( 0.022931)

如何对 shtouch 进行基准测试,同时防止输出到标准输出?

最佳答案

查看the source of sh它似乎接受 verbose 作为一个选项,所以对你来说正确的调用应该是:

sh "touch foo.txt", verbose: false

can be said of touch :

touch "bar.txt", verbose: false

文档似乎根本没有提到这些选项。 😕

关于ruby - 使用 Ruby Benchmark 时防止输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41369845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com