Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created September 15, 2012 17:31
Show Gist options
  • Save myronmarston/3728965 to your computer and use it in GitHub Desktop.
Save myronmarston/3728965 to your computer and use it in GitHub Desktop.
Benchmark showing that memoizing symbols doesn't make sense.
require 'benchmark'
def memoized_symbol
@memoized_symbol ||= :memoized_symbol
end
def raw_symbol
:raw_symbol
end
n = 10_000_000
Benchmark.bm(20) do |x|
x.report("with memoization ") do
n.times { memoized_symbol }
end
x.report("without memoization") do
n.times { raw_symbol }
end
end
=begin
Output:
➜ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
➜ ruby memoizing_symbols_benchmark.rb
user system total real
with memoization 1.640000 0.000000 1.640000 ( 1.646549)
without memoization 1.140000 0.000000 1.140000 ( 1.131850)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment