Created
August 10, 2013 18:54
-
-
Save dgutov/6201667 to your computer and use it in GitHub Desktop.
Bob vs Bob
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' | |
class Bob | |
def hey(msg) | |
case msg | |
when nil, "" | |
"Fine. Be that way!" | |
when /\A[^a-z]+\z/ | |
"Woah, chill out!" | |
when /\?\z/ | |
"Sure." | |
else | |
"Whatever." | |
end | |
end | |
end | |
class Bob2 | |
def hey(message) | |
message = Message.new(message) | |
if message.dumb? | |
'Fine. Be that way!' | |
elsif message.angry? | |
'Woah, chill out!' | |
elsif message.inquisitive? | |
'Sure.' | |
else | |
"Whatever." | |
end | |
end | |
end | |
class Message | |
def initialize(message) | |
@content = message.to_s | |
end | |
def angry? | |
@content.upcase == @content && !dumb? | |
end | |
def inquisitive? | |
@content.end_with? "?" | |
end | |
def dumb? | |
@content.lstrip.empty? | |
end | |
end | |
Benchmark.bm do |x| | |
bob = Bob.new | |
msg = "Allo " + " allo" * 1000 + " allo?" | |
x.report("my") { 100000.times { bob.hey(msg) } } | |
bob2 = Bob2.new | |
x.report("other") { 100000.times { bob2.hey(msg) } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment