Skip to content

Commit 28ecc97

Browse files
committed
Add Function with single Array argument vs splat arguments example
1 parent 833c027 commit 28ecc97

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ Comparison:
143143
&method: 467095.4 i/s - 4.00x slower
144144
```
145145

146+
##### Function with single Array argument vs splat arguments [code](code/general/array-argument-vs-splat-arguments.rb)
147+
148+
```
149+
$ ruby -v array-argument-vs-splat-argument.rb
150+
ruby 2.1.7p400 (2015-08-18 revision 51632) [x86_64-linux-gnu]
151+
Calculating -------------------------------------
152+
Function with single Array argument
153+
157.231k i/100ms
154+
Function with splat arguments
155+
4.983k i/100ms
156+
-------------------------------------------------
157+
Function with single Array argument
158+
5.581M (± 2.0%) i/s - 27.987M
159+
Function with splat arguments
160+
54.428k (± 3.3%) i/s - 274.065k
161+
162+
Comparison:
163+
Function with single Array argument: 5580972.6 i/s
164+
Function with splat arguments: 54427.7 i/s - 102.54x slower
165+
166+
```
167+
146168
### Array
147169

148170
##### `Array#bsearch` vs `Array#find` [code](code/array/bsearch-vs-find.rb)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "benchmark/ips"
2+
3+
module M
4+
ITEMS = (1..10000).to_a.freeze
5+
6+
def self.func(*args)
7+
end
8+
end
9+
10+
def fast
11+
M.func(M::ITEMS)
12+
end
13+
14+
def slow
15+
M.func(*M::ITEMS)
16+
end
17+
18+
Benchmark.ips do |x|
19+
x.report("Function with single Array argument") { fast }
20+
x.report("Function with splat arguments") { slow }
21+
x.compare!
22+
end

0 commit comments

Comments
 (0)