bench/compared_with_object_space_bench.rb (13 lines of code) (raw):

require_relative 'benchmark_helper' Allocations.start strings = 10_000.times.map { 'a' * 32 } Benchmark.ips do |bench| bench.report 'ObjectSpace' do ObjectSpace.each_object(String).count end bench.report 'Allocations' do Allocations.to_hash[String] end bench.compare! end Allocations.stop