Kurt Stephens

Nerd Up!

Recent comments

Random image

Latest image

Syndicate

Syndicate content

Browse archives

« November 2008  
Mo Tu We Th Fr Sa Su
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15
17 18 19 20 21 22
24 25 26 27 28 29 30

performance

Ruby: Performance of Symbol Construction

Kurt on Sun, 2008-11-23 03:54.

Measurements of Symbol Constructor Expressions.

n=10_000_000

ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-linux]:

> /cnu/bin/ruby symbol_benchmark.rb
                               user     system      total        real
Null Test                  0.740000   0.000000   0.740000 (  0.742914)
'foo_bar'                  1.670000   0.000000   1.670000 (  1.661374)
"foo_bar"                  1.620000   0.000000   1.620000 (  1.625221)
:foo_bar                   0.890000   0.000000   0.890000 (  0.886903)
:'foo_bar'                 0.880000   0.000000   0.880000 (  0.878555)
<br class="clear" />

Ruby : Tight Code, Floppy Performance

Kurt on Tue, 2007-10-09 20:04.

So you’re coding some ruby, and you do the obligatory caching of a computation:


def foo(x)
  666
end

$cache = nil
def cryptic_cached_foo
  ($cache ||= [ foo("bar") ]).first
end

def nicey_cached_foo
  unless $cache
    $cache = [ foo("bar") ]
  end
  $cache.first
end

A discussion came up at work: is nicey_cached_foo “better” than cryptic_cached_foo? Obviously, nicey_cached_foo is more readable, but when I find myself doing the same pattern over and over, I prefer the terse cryptic_cached_foo. I expected cryptic_cached_foo to be faster. Oh boy, was I wrong….

Syndicate content