Sha256: befeb755e59151c292c153d1c226144acfd3dd4c4543b393c2c3323277c1c20a

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'benchmark'
require_relative '../lib/hyper_iterator'

class Thing; end

puts '---------------------------------------------------------'
puts '------------------------- each! -------------------------'

puts '---------------------------------------------------------'
puts '------------------ Garbage Collection -------------------'

puts 'Array#each'
puts '----------------------'

arr = Array.new(100) { Thing.new }
line = ''

arr.each do |el|
  GC.start
  print ObjectSpace.each_object(Thing).count
  print ' '
end

puts
puts

puts 'Array#each!'
puts '----------------------'

arr = Array.new(100) { Thing.new }

arr.each! do |el|
  GC.start
  print ObjectSpace.each_object(Thing).count
  print ' '
end

puts

puts '---------------------------------------------------------'
puts '-------------------- Objects Created --------------------'


GC.disable
arr = Array.new(1000) { Thing.new }

before = ObjectSpace.count_objects
arr.each do |el|
end
after = ObjectSpace.count_objects

puts 'Array#each'
puts '----------------------'
puts "# of arrays: %d" % (after[:T_ARRAY] - before[:T_ARRAY])
puts "# of nodes: %d" % (after[:T_NODE] - before[:T_NODE])

puts

arr = Array.new(1000) { Thing.new }

before = ObjectSpace.count_objects
arr.each! do |el|
end
after = ObjectSpace.count_objects

puts 'Array#each!'
puts '----------------------'
puts "# of arrays: %d" % (after[:T_ARRAY] - before[:T_ARRAY])
puts "# of nodes: %d" % (after[:T_NODE] - before[:T_NODE])

# puts '---------------------------------------------------------'
# puts '--------------- Execution Time Comparison ---------------'

# GC.enable
# n = 10
# arr = Array.new(1_000_000) { Thing.new }

# Benchmark.bmbm(7) do |x|
#   x.report('each!') { n.times { arr.each! { |el| nil } } }
#   x.report('each')  { n.times { arr.each  { |el| nil } } }
# end

puts '---------------------------------------------------------'

puts

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hyper_iterator-0.2.0 benchmark/each_bang_bm.rb