Skip to content

Instantly share code, notes, and snippets.

@jonforums
Created March 23, 2010 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonforums/340718 to your computer and use it in GitHub Desktop.
Save jonforums/340718 to your computer and use it in GitHub Desktop.
Benchmark big reads
# redmine.ruby-lang.org issue #2742
require 'benchmark'
begin
require 'rbconfig'
puts "(#{RbConfig::CONFIG['host_os']}) #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} \
#{RbConfig::CONFIG['CC']} #{RbConfig::CONFIG['optflags']} #{RbConfig::CONFIG['debugflags']}"
rescue LoadError
end
bm_engine = case RUBY_ENGINE
when /ironruby|jruby/
:bmbm
else
:bm
end rescue :bm
Benchmark.send(bm_engine, 12) do |x|
x.report("big_read[r]") { File.open("big_foo.txt", 'r') {|io| io.read } }
x.report("big_read[rb]") { File.open("big_foo.txt", 'rb') {|io| io.read } }
if RUBY_VERSION > '1.9'
x.report("r:binary") { File.open("big_foo.txt", 'r:binary') {|io| io.read } }
end
x.report("readline[r]") do
File.open("big_foo.txt", 'r') do |io|
io.each_line {|l| l }
end
end
x.report("readline[rb]") do
File.open("big_foo.txt", 'rb') do |io|
io.each_line {|l| l }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment