Skip to content

Instantly share code, notes, and snippets.

@sferik
Created August 21, 2011 23:08
Show Gist options
  • Save sferik/1161318 to your computer and use it in GitHub Desktop.
Save sferik/1161318 to your computer and use it in GitHub Desktop.
iterations = 100
test_file = "#{File.dirname(__FILE__)}/benchmark.md"
implementations = [
{class_name: 'BlueCloth', gem_name: 'bluecloth', require_name: 'bluecloth'},
{class_name: 'RDiscount', gem_name: 'rdiscount', require_name: 'rdiscount'},
{class_name: 'Maruku', gem_name: 'maruku', require_name: 'maruku'},
{class_name: 'PEGMarkdown', gem_name: 'rpeg-markdown', require_name: 'peg_markdown'},
]
# Attempt to require each implementation and remove any that are not
# installed.
implementations.reject! do |implementation|
begin
require implementation[:require_name]
false
rescue LoadError => boom
puts "#{class_name} excluded. Try: gem install #{implementation[:gem_name]}"
true
end
end
# Grab actual class objects.
implementations.map! { |implementation| Object.const_get(implementation[:class_name]) }
def benchmark(implementation, text, iterations)
start = Time.now
iterations.times do |i|
implementation.new(text).to_html
end
Time.now - start
end
test_data = File.read(test_file)
puts "Spinning up ..."
implementations.each { |impl| benchmark(impl, test_data, 1) }
puts "Running benchmarks ..."
results =
implementations.inject([]) do |r,impl|
GC.start
r << [ impl, benchmark(impl, test_data, iterations) ]
end
puts "Results for #{iterations} iterations:"
results.each do |impl,time|
printf " %10s %09.06fs total time, %09.06fs average\n",
"#{impl}:", time, time / iterations
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment