Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
main
Breadcrumbs
fast-ruby
/
code
/
array
/
array-new-vs-fixnum-times-map.rb
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
28 lines (22 loc) · 468 Bytes
main
Breadcrumbs
fast-ruby
/
code
/
array
/
array-new-vs-fixnum-times-map.rb
Copy path
Top
File metadata and controls
Code
Blame
28 lines (22 loc) · 468 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
require 'benchmark/ips'
ELEMENTS = 9
MAX_INDEX = ELEMENTS - 1
def fast
Array.new(ELEMENTS) { |i| i }
end
def slow
(0..MAX_INDEX).map { |i| i }
end
def slower
0.upto(MAX_INDEX).map { |i| i }
end
def slowest
ELEMENTS.times.map { |i| i }
end
Benchmark.ips do |x|
x.report('Array#new') { fast }
x.report('range + map') { slow }
x.report('Integer#upto + map') { slower }
x.report('Integer#times + map') { slowest }
x.compare!
end
You can’t perform that action at this time.