Last active
December 30, 2015 13:49
-
-
Save hauntedhost/7838475 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' | |
| require 'set' | |
| def ordered_vowel_words_regex(words) | |
| valid_words = [] | |
| words.split(" ").each do |word| | |
| vowels = word.scan(/[aeiou]/) | |
| valid_words << word if vowels == vowels.sort | |
| end | |
| valid_words.join(" ") | |
| end | |
| VOWELS_ARR = %w[a e i o u] | |
| def ordered_vowel_words_array(words) | |
| valid_words = [] | |
| words.split(" ").each do |word| | |
| vowels = word.chars.select { |char| VOWELS_ARR.include? char } | |
| valid_words << word if vowels == vowels.sort | |
| end | |
| valid_words.join(" ") | |
| end | |
| VOWELS_HASH = Hash[VOWELS_ARR.map { |vowel| [vowel, true] }] | |
| def ordered_vowel_words_hash(words) | |
| valid_words = [] | |
| words.split(" ").each do |word| | |
| vowels = word.chars.select { |char| VOWELS_HASH[char] } | |
| valid_words << word if vowels == vowels.sort | |
| end | |
| valid_words.join(" ") | |
| end | |
| def ordered_vowel_words_hash_break(words) | |
| valid_words = [] | |
| words.split(" ").each do |word| | |
| prev_vowel = '' | |
| is_ordered = true | |
| word.chars.each do |letter| | |
| if VOWELS_HASH[letter] | |
| if letter < prev_vowel | |
| is_ordered = false | |
| break | |
| end | |
| prev_vowel = letter | |
| end | |
| end | |
| valid_words << word if is_ordered | |
| end | |
| valid_words.join(" ") | |
| end | |
| VOWELS_SET = VOWELS_ARR.to_set | |
| def ordered_vowel_words_set(words) | |
| valid_words = [] | |
| words.split(" ").each do |word| | |
| vowels = word.chars.select { |char| VOWELS_SET.include? char } | |
| valid_words << word if vowels == vowels.sort | |
| end | |
| valid_words.join(" ") | |
| end | |
| if __FILE__ == $0 | |
| n = 90_000 | |
| Benchmark.bm(15) do |x| | |
| x.report("regex") { n.times { ordered_vowel_words_regex("single") }} | |
| x.report("regex") { n.times { ordered_vowel_words_regex("just a short sentence") }} | |
| x.report("regex") { n.times { ordered_vowel_words_regex("this is a longer test of the vowel ordering system") }} | |
| puts | |
| x.report("array") { n.times { ordered_vowel_words_array("single") }} | |
| x.report("array") { n.times { ordered_vowel_words_array("just a short sentence") }} | |
| x.report("array") { n.times { ordered_vowel_words_array("this is a longer test of the vowel ordering system") }} | |
| puts | |
| x.report("hash") { n.times { ordered_vowel_words_hash("single") }} | |
| x.report("hash") { n.times { ordered_vowel_words_hash("just a short sentence") }} | |
| x.report("hash") { n.times { ordered_vowel_words_hash("this is a longer test of the vowel ordering system") }} | |
| puts | |
| x.report("hash_break") { n.times { ordered_vowel_words_hash_break("single") }} | |
| x.report("hash_break") { n.times { ordered_vowel_words_hash_break("just a short sentence") }} | |
| x.report("hash_break") { n.times { ordered_vowel_words_hash_break("this is a longer test of the vowel ordering system") }} | |
| puts | |
| x.report("set") { n.times { ordered_vowel_words_set("single") }} | |
| x.report("set") { n.times { ordered_vowel_words_set("just a short sentence") }} | |
| x.report("set") { n.times { ordered_vowel_words_set("this is a longer test of the vowel ordering system") }} | |
| end | |
| end | |
| # results: | |
| # ======== | |
| # $ ruby ordered_vowel_words_benchmarks.rb | |
| # user system total real | |
| # regex 0.530000 0.010000 0.540000 ( 0.535407) | |
| # regex 1.520000 0.000000 1.520000 ( 1.523413) | |
| # regex 3.390000 0.010000 3.400000 ( 3.393933) | |
| # array 0.600000 0.000000 0.600000 ( 0.593546) | |
| # array 1.840000 0.000000 1.840000 ( 1.848944) | |
| # array 4.140000 0.010000 4.150000 ( 4.144383) | |
| # hash 0.490000 0.000000 0.490000 ( 0.492928) | |
| # hash 1.520000 0.010000 1.530000 ( 1.519575) | |
| # hash 3.460000 0.010000 3.470000 ( 3.463870) | |
| # hash_break 0.440000 0.000000 0.440000 ( 0.445533) | |
| # hash_break 1.400000 0.000000 1.400000 ( 1.393313) | |
| # hash_break 2.990000 0.010000 3.000000 ( 3.004539) | |
| # set 0.500000 0.010000 0.510000 ( 0.504240) | |
| # set 1.560000 0.000000 1.560000 ( 1.563369) | |
| # set 3.610000 0.010000 3.620000 ( 3.627163) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment