bragi (owner)

Revisions

gist: 49389 Download_button fork
public
Public Clone URL: git://gist.github.com/49389.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#/usr/bin/env ruby
 
require 'benchmark'
 
def to_array_flatten(element)
  [element].flatten
end
 
def to_array_splat(element)
  [*element]
end
 
single_element = 'a'
large_array = (0..100).map {rand}
total_count = 100_000
 
Benchmark.bmbm do |b|
  b.report("Single element flatten") {total_count.times {to_array_flatten(single_element)}}
  b.report("Single element splat") {total_count.times {to_array_splat(single_element)}}
  b.report("Multiple element flatten") {total_count.times {to_array_flatten(large_array)}}
  b.report("Multiple element splat") {total_count.times {to_array_splat(large_array)}}
end