Skip to content

Instantly share code, notes, and snippets.

@miyucy
Created July 31, 2009 06:10
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 miyucy/159110 to your computer and use it in GitHub Desktop.
Save miyucy/159110 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -wKU
require 'benchmark'
dir = "/path/to/data"
Benchmark.bmbm do |job|
job.report('IO.foreach') do
File.foreach(File.join(dir, 'data.txt')) do |line|
uid, rid = line.chomp.split(/:/)
end
end
job.report('IO.readlines') do
File.readlines(File.join(dir, 'data.txt')).each do |line|
uid, rid = line.chomp.split(/:/)
end
end
job.report('IO#each') do
File.open(File.join(dir, 'data.txt'), 'rb') do |f|
f.each do |line|
uid, rid = line.chomp.split(/:/)
end
end
end
job.report('IO#gets') do
File.open(File.join(dir, 'data.txt'), 'rb') do |f|
while f.gets
uid, rid = $_.chomp.split(/:/)
end
end
end
job.report('IO#readlines') do
File.open(File.join(dir, 'data.txt'), 'rb') do |f|
f.readlines.each do |line|
uid, rid = line.chomp.split(/:/)
end
end
end
end
# >> Rehearsal ------------------------------------------------
# >> IO.foreach 1.740000 1.270000 3.010000 ( 3.608147)
# >> IO.readlines 2.390000 0.920000 3.310000 ( 3.825693)
# >> IO#each 1.680000 1.300000 2.980000 ( 3.559145)
# >> IO#gets 1.080000 1.360000 2.440000 ( 2.969118)
# >> IO#readlines 1.460000 0.610000 2.070000 ( 2.708037)
# >> -------------------------------------- total: 13.810000sec
# >>
# >> user system total real
# >> IO.foreach 1.520000 1.320000 2.840000 ( 3.827412)
# >> IO.readlines 1.740000 0.680000 2.420000 ( 2.997026)
# >> IO#each 1.460000 1.490000 2.950000 ( 3.533643)
# >> IO#gets 1.210000 1.550000 2.760000 ( 3.163758)
# >> IO#readlines 1.560000 0.520000 2.080000 ( 2.857770)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment