Last active
August 29, 2015 13:58
-
-
Save vzvu3k6k/10016989 to your computer and use it in GitHub Desktop.
A benchmark for https://github.com/zenspider/enhanced-ruby-mode/pull/49
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 'benchmark' | |
def make_hash list | |
Hash[list.map { |k| [k, true] }] | |
end | |
TRY_NUM = 1000000 | |
{ | |
INDENT_KW: [:begin, :def, :case, :module, :class, :do, :for], # largest | |
PRE_OPTIONAL_DO_KW: [:in, :while, :until] # smallest | |
}.each do |name, array| | |
puts "-- #{name} --" | |
hash = make_hash array | |
Benchmark.bm do |x| | |
x.report("array") { | |
TRY_NUM.times do | |
array.each {|i| array.include? i} | |
end | |
} | |
x.report("hash") { | |
TRY_NUM.times do | |
array.each {|i| hash.include? i} | |
end | |
} | |
end | |
end |
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 'benchmark' | |
def make_hash list | |
Hash[list.map { |k| [k, true] }] | |
end | |
TRY_NUM = 1000000 | |
{ | |
INDENT_KW: [:begin, :def, :case, :module, :class, :do, :for], | |
PRE_OPTIONAL_DO_KW: [:in, :while, :until] | |
}.each do |name, array| | |
Benchmark.bm do |x| | |
x.report(name) { | |
TRY_NUM.times do | |
make_hash array | |
end | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bm.rb
make_hash_bm.rb