Open Synapse

Hi, I'm Abhay Kumar. I like to throw up interesting things I encounter here. You can also see the result of my mustache pact with my coworkers, check out what I'm up to or see the few photos I take. I like to receive email and instant messages (AIM / Jabber), as well. Some links of interest: Calais Text Tagger, Powerset

Jul 14
Permalink

Clever but slow: Symbol#to_proc

Ever do something like this in a Rails app and think it was really awesome and convenient?

all_content = Post.find(:all).map(&:content)

Symbol#to_proc is really neat but is expensive as heck.

Here’s some sample benchmarks:

                      user     system      total        real
without to_proc   0.840000   0.000000   0.840000 (  0.858780)
with to_proc      1.780000   0.020000   1.800000 (  1.816275)

That means code that looks like this:

(1..100).inject(&:+)

looks really neat but is quite a bit slower than

(1..100).inject {|sum, n| sum + n }

ActiveRecord, your Ruby-Fu may be strong but it’s going to cramp my style.