Skip to content

Instantly share code, notes, and snippets.

@dgutov
Created August 10, 2013 18:54
Show Gist options
  • Save dgutov/6201667 to your computer and use it in GitHub Desktop.
Save dgutov/6201667 to your computer and use it in GitHub Desktop.
Bob vs Bob
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