Created
October 14, 2016 17:06
-
-
Save jrunning/da9e9e3c00bda19c7d213ceebb38de90 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/ips" | |
NUM_WORDS = 10_000 | |
file = File.open("/usr/share/dict/words") | |
# Get all the "bo" words | |
WORDS = file.each_line.grep(/^bo/).map(&:chomp) | |
file.rewind | |
puts "#{WORDS.size} \"bo\" words" | |
# Get non-"bo" words randomly until we have 10,000 | |
file.each_line do |line| | |
break if WORDS.size == NUM_WORDS | |
next if line.start_with?("bo") | |
next if rand > 0.05 | |
WORDS << line.chomp | |
end | |
puts "#{WORDS.size} words total" | |
def jordan(words) | |
words.each_index.select {|idx| words[idx].start_with?("bo") } | |
end | |
def mudasobwa(words) | |
words.each_with_index | |
.grep(->(e) { e.first.start_with? "bo" }) | |
.map(&:last) | |
end | |
def ndn(words) | |
words.map.with_index.select {|element, _| element.start_with? 'bo' }.map(&:last) | |
end | |
def scott_bartell(words) | |
words.map.with_index {|element, index| index if element.start_with? 'bo' }.compact | |
end | |
Benchmark.ips do |x| | |
x.report("Jordan") { jordan(WORDS) } | |
x.report("mudasobwa") { mudasobwa(WORDS) } | |
x.report("ndn") { ndn(WORDS) } | |
x.report("Scott Bartell") { scott_bartell(WORDS) } | |
x.compare! | |
end |
This file contains hidden or 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
$ ruby -v | |
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15] | |
$ ruby /tmp/indexes.rb | |
1122 "bo" words | |
10000 words total | |
Warming up -------------------------------------- | |
Jordan 69.000 i/100ms | |
mudasobwa 38.000 i/100ms | |
ndn 43.000 i/100ms | |
Scott Bartell 62.000 i/100ms | |
Calculating ------------------------------------- | |
Jordan 707.374 (± 3.7%) i/s - 3.588k in 5.079785s | |
mudasobwa 385.379 (± 2.6%) i/s - 1.938k in 5.032402s | |
ndn 437.519 (± 3.4%) i/s - 2.193k in 5.018273s | |
Scott Bartell 620.111 (± 3.7%) i/s - 3.100k in 5.005881s | |
Comparison: | |
Jordan: 707.4 i/s | |
Scott Bartell: 620.1 i/s - 1.14x slower | |
ndn: 437.5 i/s - 1.62x slower | |
mudasobwa: 385.4 i/s - 1.84x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Woohoo! Only 1.14x slower 😄