Skip to content

Commit 414f86f

Browse files
sshawJuanitoFatas
authored andcommitted
Add raise vs E2MM
- Add descriptopn of Exception2MessageMapper Closes #51
1 parent d64e875 commit 414f86f

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,45 @@ Comparison:
102102
module_eval with string: 1129.7 i/s - 1.19x slower
103103
```
104104

105+
##### `raise` vs `E2MM#Raise` for raising (and defining) exeptions [code](code/general/raise-vs-e2mmap.rb)
106+
107+
Ruby's [Exception2MessageMapper module](http://ruby-doc.org/stdlib-2.2.0/libdoc/e2mmap/rdoc/index.html) allows one to define and raise exceptions with predefined messages.
108+
109+
```
110+
$ ruby -v code/general/raise-vs-e2mmap.rb
111+
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
112+
113+
Calculating -------------------------------------
114+
Ruby exception: E2MM#Raise
115+
2.865k i/100ms
116+
Ruby exception: Kernel#raise
117+
42.215k i/100ms
118+
-------------------------------------------------
119+
Ruby exception: E2MM#Raise
120+
27.270k (± 8.8%) i/s - 137.520k
121+
Ruby exception: Kernel#raise
122+
617.446k (± 7.9%) i/s - 3.082M
123+
124+
Comparison:
125+
Ruby exception: Kernel#raise: 617446.2 i/s
126+
Ruby exception: E2MM#Raise: 27269.8 i/s - 22.64x slower
127+
128+
Calculating -------------------------------------
129+
Custom exception: E2MM#Raise
130+
2.807k i/100ms
131+
Custom exception: Kernel#raise
132+
45.313k i/100ms
133+
-------------------------------------------------
134+
Custom exception: E2MM#Raise
135+
29.005k (± 7.2%) i/s - 145.964k
136+
Custom exception: Kernel#raise
137+
589.149k (± 7.8%) i/s - 2.945M
138+
139+
Comparison:
140+
Custom exception: Kernel#raise: 589148.7 i/s
141+
Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
142+
```
143+
105144
#### Method Invocation
106145

107146
##### `call` vs `send` vs `method_missing` [code](code/method/call-vs-send-vs-method_missing.rb)
@@ -163,6 +202,9 @@ Comparison:
163202
Function with single Array argument: 5580972.6 i/s
164203
Function with splat arguments: 54427.7 i/s - 102.54x slower
165204
205+
Ruby's [Exception2MessageMapper module](http://ruby-doc.org/stdlib-2.2.0/libdoc/e2mmap/rdoc/index.html)
206+
allows one to define and raise exceptions with predefined messages.
207+
166208
```
167209

168210
### Array

code/general/raise-vs-e2mmap.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'benchmark/ips'
2+
require 'e2mmap'
3+
4+
class WithE2MM
5+
extend Exception2MessageMapper
6+
7+
def_e2message TypeError, 'argument must be a %s'
8+
def_exception :FooError, 'foo: %s'
9+
10+
def self.raise_ruby_defined
11+
Raise TypeError, 'Hash'
12+
end
13+
14+
def self.raise_user_defined
15+
Raise FooError, 'bar!'
16+
end
17+
end
18+
19+
class WithoutE2MM
20+
FooError = Class.new(StandardError)
21+
22+
def self.raise_ruby_defined
23+
raise TypeError, 'argument must be a Hash'
24+
end
25+
26+
def self.raise_user_defined
27+
raise FooError, 'foo: bar!'
28+
end
29+
end
30+
31+
def slow_ruby_defined
32+
begin
33+
WithE2MM.raise_ruby_defined
34+
rescue
35+
'fast ruby'
36+
end
37+
end
38+
39+
def fast_ruby_defined
40+
begin
41+
WithoutE2MM.raise_ruby_defined
42+
rescue
43+
'fast ruby'
44+
end
45+
end
46+
47+
def slow_user_defined
48+
begin
49+
WithE2MM.raise_user_defined
50+
rescue
51+
'fast ruby'
52+
end
53+
end
54+
55+
def fast_user_defined
56+
begin
57+
WithoutE2MM.raise_user_defined
58+
rescue
59+
'fast ruby'
60+
end
61+
end
62+
63+
Benchmark.ips do |x|
64+
x.report('Ruby exception: E2MM#Raise') { slow_ruby_defined }
65+
x.report('Ruby exception: Kernel#raise') { fast_ruby_defined }
66+
x.compare!
67+
end
68+
69+
Benchmark.ips do |x|
70+
x.report('Custom exception: E2MM#Raise') { slow_user_defined }
71+
x.report('Custom exception: Kernel#raise') { fast_user_defined }
72+
x.compare!
73+
end

0 commit comments

Comments
 (0)