Skip to content

Commit 1b4a509

Browse files
authored
Merge pull request #91 from Drenmi/array-new-vs-fixnum-times-map
Add benchmark for Array#new vs. Fixnum#times + map
2 parents 38f49f9 + bdfed07 commit 1b4a509

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,25 @@ Comparison:
447447
Array#insert: 0.2 i/s - 262.56x slower
448448
```
449449

450+
##### `Array#new` vs `Fixnum#times + map` [code](code/array/array-new-vs-fixnum-times-map.rb)
451+
452+
Typical slowdown is 40-60% depending on the size of the array. See the corresponding
453+
[pull request](https://github.com/JuanitoFatas/fast-ruby/pull/91/) for performance characteristics.
454+
455+
```
456+
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
457+
Calculating -------------------------------------
458+
Array#new 63.875k i/100ms
459+
Fixnum#times + map 48.010k i/100ms
460+
-------------------------------------------------
461+
Array#new 1.070M (± 2.2%) i/s - 5.365M
462+
Fixnum#times + map 678.097k (± 2.7%) i/s - 3.409M
463+
464+
Comparison:
465+
Array#new: 1069837.0 i/s
466+
Fixnum#times + map: 678097.4 i/s - 1.58x slower
467+
```
468+
450469
### Enumerable
451470

452471
##### `Enumerable#each + push` vs `Enumerable#map` [code](code/enumerable/each-push-vs-map.rb)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "benchmark/ips"
2+
3+
ELEMENTS = 9
4+
5+
def fast
6+
Array.new(ELEMENTS) { |i| i }
7+
end
8+
9+
def slow
10+
ELEMENTS.times.map { |i| i }
11+
end
12+
13+
Benchmark.ips do |x|
14+
x.report("Array#new") { fast }
15+
x.report("Fixnum#times + map") { slow }
16+
x.compare!
17+
end

0 commit comments

Comments
 (0)