Skip to content

Instantly share code, notes, and snippets.

@inkel
Created September 16, 2015 18:32
Show Gist options
  • Save inkel/d860eb86a6edf90ec787 to your computer and use it in GitHub Desktop.
Save inkel/d860eb86a6edf90ec787 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
GC.disable
require "objspace"
FMT = <<STATS.freeze
before %10d
after %10d
----------
%10d
STATS
N = Integer(ARGV.first || 10_000)
def profile(&block)
GC.start
before = ObjectSpace.count_objects[:T_STRING]
N.times { block.call }
after = ObjectSpace.count_objects[:T_STRING]
puts FMT % [before, after, (after - before)]
GC.start
end
puts "Iterating #{N} times".freeze
puts
puts "String".freeze
profile { "lorem" }
puts
puts "Frozen string".freeze
profile { "ipsum".freeze }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment