-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbenchmark.rb
72 lines (56 loc) · 1.27 KB
/
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
<<-INTRO
Question:
Is Memoist2 performant?
Answer:
Raw is a little bit slower
user system total real
raw method 2.220000 0.000000 2.220000 ( 2.216027)
memoist2 method 1.670000 0.000000 1.670000 ( 1.670678)
manual memoized (||=) 1.280000 0.000000 1.280000 ( 1.286099)
manual memoized (defined?) 1.380000 0.000000 1.380000 ( 1.372037)
INTRO
require 'benchmark'
require 'memoist2'
TIMES = 10_000_000
class MyClass
include Memoist2
def raw
(2**10)**2 # something mildly difficult
end
def memoized
1
end
memoize :memoized
def variable_cache
@variable_cache ||= 1
end
def defined_cache
unless defined?(@defined_cache)
@defined_cache = 1
end
@defined_cache
end
end
INSTANCE = MyClass.new
Benchmark.bm(30) do |x|
x.report("raw method") do
TIMES.times do
INSTANCE.raw
end
end
x.report("memoist2 method") do
TIMES.times do
INSTANCE.memoized
end
end
x.report("manual memoized (||=)") do
TIMES.times do
INSTANCE.variable_cache
end
end
x.report("manual memoized (defined?)") do
TIMES.times do
INSTANCE.defined_cache
end
end
end