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