Created
January 27, 2016 15:05
-
-
Save adymo/bf3643774ad33fd49c12 to your computer and use it in GitHub Desktop.
Ruby Nested Iterator Performance Investigation
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 'csv' | |
GC.disable | |
methods = [:all?, :any?, :chunk, :collect, :collect_concat, :compact, :count, [:cycle, 1], :delete_if, :detect, :drop_while, :each_entry, :each, :each_index, :each_key, :each_pair, :each_value, :each_with_index, [:each_with_object, Object.new], :fill, :find, :find_all, :find_index, :flat_map, [:grep, 0], :group_by, :inject, :keep_if, :map, :none?, :one?, :reduce, :reject, :reverse, :reverse_each, :rotate, :select, :shuffle, :uniq] | |
# print only commonly used | |
methods = [:all?, :any?, :collect, [:cycle, 1], :delete_if, :detect, :each, :each_index, :each_key, :each_pair, :each_value, :each_with_index, [:each_with_object, Object.new], :fill, :find, :find_all, [:grep, 0], :inject, :map, :none?, :one?, :reduce, :reject, :reverse, :reverse_each, :select] | |
creators = [ | |
def new_array | |
[0,1] | |
end, | |
def new_range | |
(0..1) | |
end, | |
def new_hash | |
{0 => 0, 1 => 1} | |
end | |
] | |
MEMO_OBJECT_TYPE = (RUBY_VERSION >= '2.3.0') ? :T_IMEMO : :T_NODE | |
data = [] | |
CSV.open("iterator_investigation.csv", "w+") do |csv| | |
csv << ["Iterator", "Array", "Range", "Hash"] | |
methods.each do |method| | |
args = nil | |
if method.class == Array | |
method, args = method | |
end | |
row = [method] | |
creators.each do |creator| | |
begin | |
before = ObjectSpace.count_objects | |
Array.new(10000).each do |i| | |
if args | |
send(creator).send(method, args) do |j| | |
end | |
else | |
send(creator).send(method) do |j| | |
end | |
end | |
end | |
after = ObjectSpace.count_objects | |
row << (after[MEMO_OBJECT_TYPE] - before[MEMO_OBJECT_TYPE]) / 10000 | |
rescue NoMethodError | |
row << "—" | |
end | |
end | |
csv << row | |
data << row | |
end | |
end | |
data.each do |row| | |
puts "<row>" | |
row.each do |col| | |
puts " <col>#{col}</col>" | |
end | |
puts "</row>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment