Skip to content

Instantly share code, notes, and snippets.

@bragi
Created January 20, 2009 07:46
Show Gist options
  • Save bragi/49389 to your computer and use it in GitHub Desktop.
Save bragi/49389 to your computer and use it in GitHub Desktop.
#/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment