ruby 简单的基准2红宝石方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 简单的基准2红宝石方法相关的知识,希望对你有一定的参考价值。

require 'benchmark'

iterations = 100_000

Benchmark.bm(27) do |bm|
  bm.report('finding in hash') do
    iterations.times do
      arr.find{|h| h['start_date'] == '2018'}
    end
  end

  bm.report('detect in hash') do
    iterations.times do
     arr.detect{|h| h['start_date'] == '2018'}
    end
  end

  bm.report('select in hash') do
    iterations.times do
      arr.select{|h| h['start_date'] == '2018'}
    end
  end
end

以上是关于ruby 简单的基准2红宝石方法的主要内容,如果未能解决你的问题,请参考以下文章