Created
January 20, 2009 07:46
-
-
Save bragi/49389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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