Skip to content

Instantly share code, notes, and snippets.

@jdhuntington
Created February 15, 2011 19:00
Show Gist options
  • Save jdhuntington/828007 to your computer and use it in GitHub Desktop.
Save jdhuntington/828007 to your computer and use it in GitHub Desktop.
Testing whether or not Enumerable#inject works well with a Hash.
require 'rubygems'
require 'ruby-prof'
require 'digest/md5'
$x = (1..100_000).to_a.map { |x| Digest::MD5.hexdigest(x.to_s) }
abort "Usage: #{__FILE__} (time|allocations) (inject|each)" if ARGV.length != 2
if ARGV[0] == 'time'
RubyProf.measure_mode = RubyProf::PROCESS_TIME
elsif ARGV[0] == 'allocations'
RubyProf.measure_mode = RubyProf::ALLOCATIONS
else
abort "I don't know what to do!"
end
result = RubyProf.profile do
if ARGV.last == 'inject'
entries = $x.inject({}) do |h, q|
h[q] = 'some string'
h
end
elsif ARGV[1] == 'each'
h = {}
$x.each { |x| h[x] = 'some string' }
else
abort "I don't know what to do!"
end
end
printer = RubyProf::GraphPrinter.new(result)
printer.print(STDOUT, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment