File.read的Ruby性能

时间:2013-12-22 15:25:04

标签: ruby string performance io

给出以下脚本:

require 'rubygems'
require 'open-uri'
require 'benchmark'

response = open('http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog')

outside = Benchmark.measure do
  response_body = response.read
  10000.times do
    response_body.scan(/dog/)
  end
end

inside = Benchmark.measure do
  10000.times do
    response.read.scan(/dog/)
  end
end

puts [outside, inside].map(&:utime).inspect

我得到以下结果:

[1.25, 0.06000000000000005]

为什么每次读取文件的效果要好20倍?

如果我的系统信息很重要:

ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]

1 个答案:

答案 0 :(得分:4)

这是因为在第一次测试之后,response被读到最后,并且在第二次测试的每次迭代中,read的结果都是微不足道的,这节省了时间,并且它也返回仅仅空字符串。所以scan也快结束了。

irb> response.read.scan(/dog/)
=> ["dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog"]
irb> response.read.scan(/dog/)
=> []