Skip to content

Instantly share code, notes, and snippets.

@janklimo
Created December 10, 2020 08:59
Show Gist options
  • Save janklimo/78bf13e27da06f651e49bbea028d9df3 to your computer and use it in GitHub Desktop.
Save janklimo/78bf13e27da06f651e49bbea028d9df3 to your computer and use it in GitHub Desktop.
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