蕲春人 - 软件开发

简单就是美.

Benchmark用法

Benchmark可以用来测试某个函数或者任何代码的执行时间。

用法很简单:

    require 'benchmark'
n = 1500
Benchmark.bmbm do |x|
    x.report("+ method") { n.times {string1 = "ddd" + "11111" + "Helodd" } }
    x.report("<< method") { n.times {string2 = "ddd" << "11111" << "Helodd"} }  
end
  

结果如下:

    usermatoMacBook-Pro:test qichunren$ ruby test.rb 
Rehearsal ---------------------------------------------
+ method    0.000000   0.000000   0.000000 (  0.001722)
<< method   0.000000   0.000000   0.000000 (  0.001073)
------------------------------------ total: 0.000000sec

                user     system      total        real
+ method    0.000000   0.000000   0.000000 (  0.001040)
<< method   0.000000   0.000000   0.000000 (  0.000719)
  

参考资料