Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Created July 9, 2010 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjsingleton/469567 to your computer and use it in GitHub Desktop.
Save tjsingleton/469567 to your computer and use it in GitHub Desktop.
benchmark showing that symbols live forever
100000 Strings
After Block: 3318
After Forced GC: 31
100000 Symobls
After Block: 1519
After Forced GC: 98619
N = 100_000
puts "#{N} Strings"
GC.start
start = ObjectSpace.count_objects[:T_STRING]
N.times {|i| "#{i}" }
pre_gc_count = ObjectSpace.count_objects[:T_STRING] - start
GC.start
post_gc_count = ObjectSpace.count_objects[:T_STRING] - start
puts "After Block: #{pre_gc_count}"
puts "After Forced GC: #{post_gc_count}"
puts
puts "#{N} Symbols"
GC.start
pre_gc_count = Symbol.all_symbols.count
N.times {|i| "#{i}".intern }
diff = Symbol.all_symbols.count - start
GC.start
post_gc_count = Symbol.all_symbols.count - start
puts "After Block: #{pre_gc_count}"
puts "After Forced GC: #{post_gc_count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment