-
Notifications
You must be signed in to change notification settings - Fork 376
/
raise-vs-e2mmap.rb
73 lines (60 loc) · 1.24 KB
/
raise-vs-e2mmap.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
73
require 'benchmark/ips'
require 'e2mmap'
class WithE2MM
extend Exception2MessageMapper
def_e2message TypeError, 'argument must be a %s'
def_exception :FooError, 'foo: %s'
def self.raise_ruby_defined
Raise TypeError, 'Hash'
end
def self.raise_user_defined
Raise FooError, 'bar!'
end
end
class WithoutE2MM
FooError = Class.new(StandardError)
def self.raise_ruby_defined
raise TypeError, 'argument must be a Hash'
end
def self.raise_user_defined
raise FooError, 'foo: bar!'
end
end
def slow_ruby_defined
begin
WithE2MM.raise_ruby_defined
rescue
'fast ruby'
end
end
def fast_ruby_defined
begin
WithoutE2MM.raise_ruby_defined
rescue
'fast ruby'
end
end
def slow_user_defined
begin
WithE2MM.raise_user_defined
rescue
'fast ruby'
end
end
def fast_user_defined
begin
WithoutE2MM.raise_user_defined
rescue
'fast ruby'
end
end
Benchmark.ips do |x|
x.report('Ruby exception: E2MM#Raise') { slow_ruby_defined }
x.report('Ruby exception: Kernel#raise') { fast_ruby_defined }
x.compare!
end
Benchmark.ips do |x|
x.report('Custom exception: E2MM#Raise') { slow_user_defined }
x.report('Custom exception: Kernel#raise') { fast_user_defined }
x.compare!
end