Created
January 18, 2021 05:11
-
-
Save baweaver/325a5ee1713b72c9719f72429aafdbe9 to your computer and use it in GitHub Desktop.
Benchmark to measure speed difference between Array for matching and implied matching through `to_a` on Enumerable
This file contains hidden or 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
require 'benchmark/ips' | |
module Enumerable | |
def deconstruct() = to_a | |
end | |
class Collection | |
attr_reader :items | |
include Enumerable | |
def initialize(*items) | |
@items = items | |
end | |
def each(&fn) = @items.each { yield _1 } | |
end | |
CollectionStruct = Struct.new(:items) do | |
def deconstruct() = self.items | |
end | |
small_array = (1..25).to_a.shuffle | |
small_collection = Collection.new(*small_array) | |
small_struct_collection = CollectionStruct.new(small_array) | |
large_array = (1..10_000).to_a.shuffle | |
large_collection = Collection.new(*large_array) | |
large_struct_collection = CollectionStruct.new(large_array) | |
p( | |
small_array: (small_array in [*, 20, *]), | |
small_collection: (small_collection in [*, 20, *]), | |
small_struct_collection: (small_struct_collection in [*, 20, *]), | |
large_array: (large_array in [*, 2000, *]), | |
large_collection: (large_collection in [*, 2000, *]), | |
large_struct_collection: (large_struct_collection in [*, 2000, *]), | |
) | |
Benchmark.ips do |x| | |
x.report('Small Array') { small_array in [*, 20, *] } | |
x.report('Small Collection') { small_collection in [*, 20, *] } | |
x.report('Small Struct Collection') { small_struct_collection in [*, 20, *] } | |
x.report('Large Array') { large_array in [*, 2000, *] } | |
x.report('Large Collection') { large_collection in [*, 2000, *] } | |
x.report('Large Struct Collection') { large_struct_collection in [*, 2000, *] } | |
end | |
# {:small_array=>true, :small_collection=>true, :small_struct_collection=>true, :large_array=>true, :large_collection=>true, :large_struct_collection=>true} | |
# Warming up -------------------------------------- | |
# Small Array 83.912k i/100ms | |
# Small Collection 33.239k i/100ms | |
# Small Struct Collection | |
# 83.185k i/100ms | |
# Large Array 307.000 i/100ms | |
# Large Collection 109.000 i/100ms | |
# Large Struct Collection | |
# 306.000 i/100ms | |
# Calculating ------------------------------------- | |
# Small Array 830.450k (± 3.8%) i/s - 4.196M in 5.059874s | |
# Small Collection 338.283k (± 3.1%) i/s - 1.695M in 5.016166s | |
# Small Struct Collection | |
# 777.924k (± 4.1%) i/s - 3.910M in 5.034506s | |
# Large Array 2.831k (± 4.1%) i/s - 14.429k in 5.105557s | |
# Large Collection 1.087k (± 3.6%) i/s - 5.450k in 5.020147s | |
# Large Struct Collection | |
# 2.925k (± 5.1%) i/s - 14.688k in 5.034629s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment