Created
February 15, 2011 19:00
-
-
Save jdhuntington/828007 to your computer and use it in GitHub Desktop.
Testing whether or not Enumerable#inject works well with a Hash.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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