Sha256: 2166b9502f71d90d9a7ddb72605fcb96566c5093c66a0df79cecca7c5ef08656

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 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

4 entries across 4 versions & 1 rubygems

Version Path
hyper_iterator-0.3.1 benchmark/each_bang_bm.rb
hyper_iterator-0.3.0 benchmark/each_bang_bm.rb
hyper_iterator-0.2.2 benchmark/each_bang_bm.rb
hyper_iterator-0.2.1 benchmark/each_bang_bm.rb