Skip to content

Instantly share code, notes, and snippets.

@raine
Created May 24, 2010 08:58
Show Gist options
  • Save raine/411665 to your computer and use it in GitHub Desktop.
Save raine/411665 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# NIEC BENCHMARKER:
#
# == Usage
#
# 1. Create a ruby source file containing methods with arbitrary names.
# Those are the methods that will be benchmarked.
# For example, contents of my-benchmarks.rb:
#
# def do_stuff; 123; end;
# def do_other_stuff; 321; end;
#
# 2. ./benchmark.rb my-benchmarks.rb
#
# user system total real
# do_stuff 0.040000 0.000000 0.040000 ( 0.043305)
# do_other_stuff 0.040000 0.000000 0.040000 ( 0.043316)
#
require 'benchmark'
unless ARGV[0].match(/\.rb/)
puts "error: no source file given"
exit
end
path = ARGV[0]
file = File.read(path)
tmp = Object.private_methods
n = (ARGV.grep(/^\d+$/)[0] || 100000).to_i
eval(file)
bm_methods = Object.private_methods - tmp
Benchmark.bm(bm_methods.map(&:size).max) do |x|
bm_methods.each do |m|
x.report(m.to_s) { n.times { Object.send(m) } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment