Created
September 15, 2012 17:31
-
-
Save myronmarston/3728965 to your computer and use it in GitHub Desktop.
Benchmark showing that memoizing symbols doesn't make sense.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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