Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created February 22, 2010 08:43
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 rklemme/310932 to your computer and use it in GitHub Desktop.
Save rklemme/310932 to your computer and use it in GitHub Desktop.
File reading benchmark
#! ruby19
require 'benchmark'
REP = 10
BS = 1024
files = []
at_exit do
files.each {|name| File.delete(name) rescue nil}
end
[100, 1024*1024, 1024*1024*100].each do |s|
name = "f-#{s}.bin"
File.open(name, File::WRONLY | File::EXCL | File::CREAT | File::BINARY) do |out|
data = (".".encode! 'binary') * 1024
while s >= data.bytesize
s -= out.write(data)
end
out.write(data[0, s])
end
files << name
end
Benchmark.bmbm 30 do |r|
files.each do |name|
r.report name + ' simple' do
REP.times do
File.open name, "rb" do |io|
while b = io.read(BS)
end
end
end
end
r.report name + ' smart' do
REP.times do
File.open name, "rb" do |io|
b = ""
while io.read(BS, b)
end
end
end
end
r.report name + ' complete' do
REP.times do
File.open name, "rb" do |io|
io.read
end
end
end
end
end
Rehearsal -----------------------------------------------------------------
f-100.bin simple 0.000000 0.000000 0.000000 ( 0.004000)
f-100.bin smart 0.016000 0.000000 0.016000 ( 0.005000)
f-100.bin complete 0.000000 0.000000 0.000000 ( 0.008000)
f-1048576.bin simple 0.094000 0.046000 0.140000 ( 0.133000)
f-1048576.bin smart 0.078000 0.016000 0.094000 ( 0.100000)
f-1048576.bin complete 0.015000 0.031000 0.046000 ( 0.054000)
f-104857600.bin simple 9.031000 3.875000 12.906000 ( 12.913000)
f-104857600.bin smart 5.766000 4.047000 9.813000 ( 9.820000)
f-104857600.bin complete 0.609000 3.156000 3.765000 ( 3.807000)
------------------------------------------------------- total: 26.780000sec
user system total real
f-100.bin simple 0.016000 0.000000 0.016000 ( 0.007000)
f-100.bin smart 0.000000 0.000000 0.000000 ( 0.008000)
f-100.bin complete 0.000000 0.000000 0.000000 ( 0.007000)
f-1048576.bin simple 0.093000 0.109000 0.202000 ( 0.207000)
f-1048576.bin smart 0.078000 0.016000 0.094000 ( 0.103000)
f-1048576.bin complete 0.000000 0.046000 0.046000 ( 0.037000)
f-104857600.bin simple 8.594000 4.125000 12.719000 ( 12.760000)
f-104857600.bin smart 6.219000 3.500000 9.719000 ( 9.721000)
f-104857600.bin complete 0.593000 3.125000 3.718000 ( 3.778000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment