|
| 1 | +require 'memory_profiler' |
| 2 | + |
| 3 | +report_1 = MemoryProfiler.report do |
| 4 | + def get_me_directly |
| 5 | + File.read('1.txt') |
| 6 | + end |
| 7 | + 100.times { get_me_directly } |
| 8 | +end |
| 9 | + |
| 10 | +report_2 = MemoryProfiler.report do |
| 11 | + ST = File.read('2.txt') |
| 12 | + def get_me_with_constant |
| 13 | + ST |
| 14 | + end |
| 15 | + 100.times { get_me_with_constant } |
| 16 | +end |
| 17 | + |
| 18 | +report_3 = MemoryProfiler.report do |
| 19 | + ST_FREEZE = File.read('3.txt').freeze |
| 20 | + def get_me_with_constant_freeze |
| 21 | + ST_FREEZE |
| 22 | + end |
| 23 | + 100.times { get_me_with_constant_freeze } |
| 24 | +end |
| 25 | + |
| 26 | +puts ' With get_me_directly '.center(50, '✨') |
| 27 | +report_1.pretty_print |
| 28 | +# Allocated String Report |
| 29 | +# ----------------------------------- |
| 30 | +# 200 "1.txt" |
| 31 | +# 200 memory_freeze_benchmark.rb:5 |
| 32 | +# |
| 33 | +# 100 "" |
| 34 | +# 100 memory_freeze_benchmark.rb:5 |
| 35 | +# |
| 36 | +# |
| 37 | +# Retained String Report |
| 38 | +# ----------------------------------- |
| 39 | + |
| 40 | +puts "\n" |
| 41 | +puts ' With get_me_with_constant '.center(50, '✨') |
| 42 | +report_2.pretty_print |
| 43 | +# Allocated String Report |
| 44 | +# ----------------------------------- |
| 45 | +# 2 "2.txt" |
| 46 | +# 2 memory_freeze_benchmark.rb:11 |
| 47 | +# |
| 48 | +# 1 "" |
| 49 | +# 1 memory_freeze_benchmark.rb:11 |
| 50 | +# |
| 51 | +# |
| 52 | +# Retained String Report |
| 53 | +# ----------------------------------- |
| 54 | +# 1 "" |
| 55 | +# 1 memory_freeze_benchmark.rb:11 |
| 56 | + |
| 57 | +puts "\n" |
| 58 | +puts ' With get_me_with_constant_freeze '.center(50, '✨') |
| 59 | +report_3.pretty_print |
| 60 | +# Allocated String Report |
| 61 | +# ----------------------------------- |
| 62 | +# 2 "3.txt" |
| 63 | +# 2 memory_freeze_benchmark.rb:19 |
| 64 | +# |
| 65 | +# 1 "" |
| 66 | +# 1 memory_freeze_benchmark.rb:19 |
| 67 | +# |
| 68 | +# |
| 69 | +# Retained String Report |
| 70 | +# ----------------------------------- |
| 71 | +# 1 "" |
| 72 | +# 1 memory_freeze_benchmark.rb:19 |
0 commit comments