Last active
January 28, 2022 14:22
-
-
Save schneems/08faee64c033c51e7f79e94381bfbc07 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
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