Skip to content

Instantly share code, notes, and snippets.

@janklimo
Created Dec 10, 2020
Embed
What would you like to do?
Ruby: try vs &.
# frozen_string_literal: true
require 'benchmark/ips'
require 'memory_profiler'
p '=== Memory when using `try` ==='
report = MemoryProfiler.report do
nil.try(:a)
end
report.pretty_print
p '=== Memory when using `&.` ==='
report = MemoryProfiler.report do
nil&.a
end
report.pretty_print
Benchmark.ips do |x|
x.report('try') { nil.try(:a) }
x.report('&.') { nil&.a }
x.compare!
end
"=== Memory when using `try` ==="
Total allocated: 40 bytes (1 objects)
Total retained: 0 bytes (0 objects)
allocated memory by gem
-----------------------------------
40 other
allocated memory by file
-----------------------------------
40 test.rb
allocated memory by location
-----------------------------------
40 test.rb:8
allocated memory by class
-----------------------------------
40 Array
allocated objects by gem
-----------------------------------
1 other
allocated objects by file
-----------------------------------
1 test.rb
allocated objects by location
-----------------------------------
1 test.rb:8
allocated objects by class
-----------------------------------
1 Array
retained memory by gem
-----------------------------------
NO DATA
retained memory by file
-----------------------------------
NO DATA
retained memory by location
-----------------------------------
NO DATA
retained memory by class
-----------------------------------
NO DATA
retained objects by gem
-----------------------------------
NO DATA
retained objects by file
-----------------------------------
NO DATA
retained objects by location
-----------------------------------
NO DATA
retained objects by class
-----------------------------------
NO DATA
"=== Memory when using `&.` ==="
Total allocated: 0 bytes (0 objects)
Total retained: 0 bytes (0 objects)
allocated memory by gem
-----------------------------------
NO DATA
allocated memory by file
-----------------------------------
NO DATA
allocated memory by location
-----------------------------------
NO DATA
allocated memory by class
-----------------------------------
NO DATA
allocated objects by gem
-----------------------------------
NO DATA
allocated objects by file
-----------------------------------
NO DATA
allocated objects by location
-----------------------------------
NO DATA
allocated objects by class
-----------------------------------
NO DATA
retained memory by gem
-----------------------------------
NO DATA
retained memory by file
-----------------------------------
NO DATA
retained memory by location
-----------------------------------
NO DATA
retained memory by class
-----------------------------------
NO DATA
retained objects by gem
-----------------------------------
NO DATA
retained objects by file
-----------------------------------
NO DATA
retained objects by location
-----------------------------------
NO DATA
retained objects by class
-----------------------------------
NO DATA
Warming up --------------------------------------
try 692.520k i/100ms
&. 2.685M i/100ms
Calculating -------------------------------------
try 7.960M (± 1.5%) i/s - 40.166M in 5.047144s
&. 27.077M (± 1.6%) i/s - 136.958M in 5.059503s
Comparison:
&.: 27076505.4 i/s
try: 7960082.8 i/s - 3.40x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment