而benchmark則可以幫我們測量一下時間
require 'benchmark'
def yyield
yield "a"
yield "b"
end
Benchmark.bm do |x|
x.report {
yyield do |k|
puts k
end
}
end
def block(&block)
block.call("a")
block.call("b")
end
Benchmark.bm do |x|
x.report {
block do |k|
puts k
end
}
end
第一次結果是
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000858)
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000231)
之後
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000030)
user system total real
a
b
0.000000 0.000000 0.000000 ( 0.000024)
沒有留言:
張貼留言