Last active
August 29, 2015 13:57
-
-
Save robinclart/9431901 to your computer and use it in GitHub Desktop.
Randomize the content within brackets in the form of [ a | b ]
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" | |
# Originally written by @blazeeboy | |
# See https://gist.github.com/9429208 | |
def generate(result) | |
# ... | |
end | |
def randomize_sentence(sentence) | |
randomized_sentence = sentence.gsub(/\[[^\[\]]+\]/) { |s| s[1..-2].split("|").shuffle.first } | |
randomized_sentence == sentence ? randomized_sentence : randomize_sentence(randomized_sentence) | |
end | |
sentence = "[Hello|Hi] my [friend|dear], how [are you [today|these days]|is your brother]!" | |
Benchmark.bm do |x| | |
x.report "Emad " do | |
10000.times { generate(sentence) } | |
end | |
x.report "Robin" do | |
10000.times { randomize_sentence(sentence) } | |
end | |
end | |
# >> user system total real | |
# >> Emad 0.530000 0.000000 0.530000 ( 0.532713) | |
# >> Robin 0.160000 0.000000 0.160000 ( 0.158647) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment