2016-12-28 7 views
2

私は、Rakeが含むシステムtouchとシステムtouchへのシェルリングを比較しようとしています。Rubyベンチマーク使用中の出力を防止する

各操作は、標準出力に出力を送信している:

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) 

または結果のみを持っている何か他のもの。

私はBenchmark.measureを使用するanother questionに読みますが、以下も動作しません:

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) 

をIベンチマークshtouchんが、stdoutに行くからの出力を防ぐ方法?

答えて

3

the source of shを見ると、あなたのために右の呼び出しはする必要がありますので、それはオプションとしてverboseを受け入れるようだ:

sh "touch foo.txt", verbose: false 

can be said of touch同じ:

touch "bar.txt", verbose: false 

ドキュメントが言及していないようですこれらのオプションはまったくありません。

0

あなたは

1 
3 

を印刷します。しかし2

+0

なぜdownvoteを印刷しません

puts 1 original_stdout = $stdout $stdout = open("/dev/null", "w") puts 2 $stdout = original_stdout puts 3 

この例を参照してください$stdout

をリダイレクトすることができますか?私は答えを改善できるようにコメントしてください。 – akuhn

関連する問題