Skip to content

Instantly share code, notes, and snippets.

@marksands
Created February 27, 2010 04:24
Show Gist options
  • Save marksands/316474 to your computer and use it in GitHub Desktop.
Save marksands/316474 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'rdiscount'
FILE = 'README.markdown'
Benchmark.bm do |b|
b.report("File.read ") do
mdown = File.read(FILE)
RDiscount.new(mdown).to_html
end
b.report("File.open.read ") do
mdown = File.open(FILE, 'r').read
RDiscount.new(mdown).to_html
end
#SUCKS
b.report("File.open 1nest += ") do
mdown = ''
File.open(FILE, "r").each_line do |line|
mdown += line
end
RDiscount.new(mdown).to_html
end
#SUCKS
b.report("File.open 1nest << ") do
mdown = ''
File.open(FILE, "r").each_line do |line|
mdown << line
end
RDiscount.new(mdown).to_html
end
#SUCKS
b.report("File.open 2nested += ") do
mdown = ''
File.open(FILE, 'r') { |f|
f.each_line do |line|
mdown += line
end
}
RDiscount.new(mdown).to_html
end
b.report("File.open 2nested << ") do
mdown = ''
File.open(FILE, 'r') { |f|
f.each_line do |line|
mdown << line
end
}
RDiscount.new(mdown).to_html
end
end
# user system total real
# File.read 0.000000 0.000000 0.000000 ( 0.001659)
# File.open.read 0.000000 0.000000 0.000000 ( 0.001477)
# File.open 1nest += 0.020000 0.010000 0.030000 ( 0.024936)
# File.open 1nest << 0.000000 0.000000 0.000000 ( 0.002105)
# File.open 2nested += 0.020000 0.000000 0.020000 ( 0.021238)
# File.open 2nested << 0.000000 0.000000 0.000000 ( 0.002230)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment