Skip to content

Instantly share code, notes, and snippets.

@schneems
Last active January 28, 2022 14:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schneems/08faee64c033c51e7f79e94381bfbc07 to your computer and use it in GitHub Desktop.
Save schneems/08faee64c033c51e7f79e94381bfbc07 to your computer and use it in GitHub Desktop.
class Add
attr_reader :result, :is_negative
def initialize(a, b)
@result = a + b
@is_negative = @result < 0
end
end
def add(a,b)
result = a + b
{ result: result, is_negative: (result < 0) }
end
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("obj return") { foo = Add.new(1, 2); foo.result; foo.is_negative }
x.report("hash return") { foo = add(1, 2); foo[:result]; foo[:is_negative] }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment