Dav*_*ave 6 ruby indexing benchmarking ruby-on-rails
我正在使用 Rails 5.0.1。使用 Rails 控制台,我想弄清楚获取字符串中第一个正则表达式出现的索引的最快方法是什么。所以我尝试了以下方法
2.4.0 :004 > Benchmark.bm do |x|
2.4.0 :005 > x.report { 50000.times { a = 'a@b.c'.index(/\@/) } }
2.4.0 :006?> x.report { 50000.times { a = 'a@b.c'.match(/\@/)[0] } }
2.4.0 :007?> end
user system total real
0.030000 0.000000 0.030000 ( 0.026763)
0.060000 0.000000 0.060000 ( 0.064986)
=> [#<Benchmark::Tms:0x007fe974e13f88 @label="", @real=0.026763000059872866, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.03000000000000025, @total=0.03000000000000025>, #<Benchmark::Tms:0x007fe973c9e9d8 @label="", @real=0.06498600030317903, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.06000000000000005, @total=0.06000000000000005>]
Run Code Online (Sandbox Code Playgroud)
我对如何阅读结果感到困惑。我“认为”它告诉我第一种方法平均需要“0.02”秒,而第二种方法需要“0.06”秒,所以我应该坚持使用“index”。我读得对吗?
您可以使用总计列。该时间不是平均值,而是该块执行所用的时间