# Symbol.to\_proc is way slower than invoking methods directly

**URL:** <https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365>\
**Category:** Rails\
**Created:** [July 29, 2008, 12:58am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365 "2008-07-29T00:58:59Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 12:58am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/1 "2008-07-29T00:58:59Z")

</div>

All,

I was recently introduced to the Symbol.to\_proc trick where you can  
invoke methods on collection elements in a map call with less syntax, as  
in:

[“1”, “2”, “3”].map(&:to\_i)

While this is very clever looking (albeit potentially harder to read for  
Ruby/Rails noobs, which implies a human performance cost), I started  
thinking about it and decided this had to be slower than calling methods  
directly, as in:

[“1”, “2”, “3”].map {|x| x.to\_i}

So I decided to benchmark it to see. My benchmark test shows that  
calling the method through the Symbol.to\_proc method (at least in this  
case) is 8.5X slower than doing the more readable method invocation.

My benchmark code is below. Is it a valid test? And if so, why should  
we use Symbol.to\_proc unnecessarily when it is such a poor performer?

Thanks,  
Wes

==========benchmark code============

#!/usr/bin/ruby

require ‘benchmark’  
include Benchmark

LOOP\_COUNT = 1\_000\_000  
x = [“1”,“2”,“3”]  
bmbm do |test|  
test.report(“Method invoke”) do  
LOOP\_COUNT.times {x.map {|elem| elem.to\_i}}  
end

test.report(“Symbol.to\_proc”) do  
LOOP\_COUNT.times {x.map(&:to\_i)}  
end  
end  
~

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 1:00am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/2 "2008-07-29T01:00:09Z")

</div>

One more thing - here’s how I ran it:

## $ script/runner -e development benchmark\_to\_proc.rb Rehearsal

Method invoke 1.600000 0.000000 1.600000 ( 1.783758)  
Symbol.to\_proc 13.240000 0.070000 13.310000 ( 13.770514)  
---------------------------------------- total: 14.910000sec

```
                 user system total real

```

Method invoke 1.590000 0.010000 1.600000 ( 1.628473)  
Symbol.to\_proc 13.210000 0.060000 13.270000 ( 13.678768)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 1:13am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/3 "2008-07-29T01:13:25Z")

</div>

Hi –

On Tue, 29 Jul 2008, Wes G. wrote:

> Ruby/Rails noobs, which implies a human performance cost), I started  
> we use Symbol.to\_proc unnecessarily when it is such a poor performer?  
> The good news, I guess, is that it seems to be only about twice as  
> slow in Ruby 1.9:

$ ruby19 to\_proc.rb  
Rehearsal --------------------------------------------------  
Method invoke 1.480000 0.010000 1.490000 ( 1.498664)  
Symbol.to\_proc 2.640000 0.010000 2.650000 ( 2.656658)  
----------------------------------------- total: 4.140000sec

```
                  user system total real

```

Method invoke 1.470000 0.010000 1.480000 ( 1.483069)  
Symbol.to\_proc 2.640000 0.010000 2.650000 ( 2.668746)

$ ruby19 -v  
ruby 1.9.0 (2008-07-20 revision 16244) [i686-darwin9.3.0]

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 2:53am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/4 "2008-07-29T02:53:21Z")

</div>

Still, twice as slow isn’t really acceptable.

Since I was told about this I’ve stopped using symbol to proc in  
favour of typing it out. It may use a little bit more time, but that’s  
time that my users won’t spend waiting for the page to process.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 2:55am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/5 "2008-07-29T02:55:42Z")

</div>

Hi –

On Tue, 29 Jul 2008, Ryan B. wrote:

> Still, twice as slow isn’t really acceptable.

I’m not thrilled either. I’m indifferent to the idiom itself, so if  
there’s much of an admission price I’m unlikely to use it. We’ll see  
how it plays out in further versions of 1.9.

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/6 "2008-07-29T12:21:10Z")

</div>

Hi –

On Mon, 28 Jul 2008, Phlip wrote:

> Wes G. wrote:
> 
> > My benchmark code is below. Is it a valid test? And if so, why should  
> > we use Symbol.to\_proc unnecessarily when it is such a poor performer?
> 
> You seem to have reproduced the research of Matz & Co. The next version of Ruby  
> will implement Symbol.to\_proc in C - turning it from a hack into a /de-facto/  
> keyword.

See my 1.9.0 benchmarks, a few posts back. It’s still (as of then) a  
good bit slower than the block version, at least in this test.

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 5:25am UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/7 "2008-07-29T05:25:23Z")

</div>

Wes G. wrote:

> My benchmark code is below. Is it a valid test? And if so, why should  
> we use Symbol.to\_proc unnecessarily when it is such a poor performer?

You seem to have reproduced the research of Matz & Co. The next version  
of Ruby  
will implement Symbol.to\_proc in C - turning it from a hack into a  
/de-facto/  
keyword.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/8 "2008-07-29T15:33:04Z")

</div>

Phlip wrote:

> Do you have a guess why? It seems both expressions could resolve to the  
> same  
> opcodes…

Isn’t it as simply as two more method calls in the &:to\_i case? One  
call to Symbol.to\_proc and another call to Object.send, in addition to  
the actual method call that you want.

When I look at the &: idiom, the only good reason I can think of to use  
it would be if you truly needed to invoke a method whose name you didn’t  
know until runtime. Even then, I suspect that &: would still be slower,  
again, because it inserts another (unnecessary) method call. In the  
case of a truly dynamic method invocation, you would probably just use  
Object.send, but with &:, you still have to do the call to  
Symbol.to\_proc before that.

This has been a good way to get me thinking about the potential cost of  
the syntactic sugar that we may exploit all the time in Rails.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/9 "2008-07-29T13:25:05Z")

</div>

David A. Black wrote:

> See my 1.9.0 benchmarks, a few posts back. It’s still (as of then) a  
> good bit slower than the block version, at least in this test.

Do you have a guess why? It seems both expressions could resolve to the  
same  
opcodes…

–  
Phlip

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/10 "2008-07-29T16:42:08Z")

</div>

Whenever you make a design decision there will be tradeoffs.  
Symbol#to\_proc trades an increase in readability for a decrease in  
performance. It’s good to be aware of this when using it. On the other  
hand, there are many things in your Rails app that will be orders of  
magnitude slower than your uses of Symbol#to\_proc, so I wouldn’t  
necessarily reject it out of hand on performance grounds alone.

If you’re writing lower level libraries, I would definitely avoid  
using Symbol#to\_proc. For a Rails app, trying to eek out milliseconds  
by replacing Symbol#to\_proc with a block is probably a poor  
optimization.

Rein

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/11 "2008-07-29T17:06:58Z")

</div>

Rein H. wrote:

> Whenever you make a design decision there will be tradeoffs.  
> Symbol#to\_proc trades an increase in readability for a decrease in  
> performance.

That’s interesting. I think that &: is way _less_ readable than the  
“regular” block syntax.

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/12 "2008-07-29T16:30:58Z")

</div>

Wes G. wrote:

> When I look at the &: idiom, the only good reason I can think of to use  
> it would be if you truly needed to invoke a method whose name you didn’t  
> know until runtime.

Premature optimization is the root of all evil. We cache both our  
database hits  
and our web pages hits. We value development time enough to not worry  
about the  
microseconds of each statement we write, so long as they save  
mega-seconds of  
coding time.

> Even then, I suspect that &: would still be slower,  
> again, because it inserts another (unnecessary) method call. In the  
> case of a truly dynamic method invocation, you would probably just use  
> Object.send, but with &:, you still have to do the call to  
> Symbol.to\_proc before that.

I can’t see a problem with making &: into a single operator. Thats how  
constructs like x[0] ||= 0 work - the entire sequence from the [ to the  
= goes  
into an operator, with x, 0, and 0 as arguments.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/13 "2008-07-29T18:28:46Z")

</div>

Hi –

On Tue, 29 Jul 2008, Phlip wrote:

> coding time.  
> Not all optimization is premature, though, and some is sort of  
> incidental. Every time you write code one way, it’s a decision not to  
> write it in n other possible ways. It’s even true that if I write:

s = “string”

I’ve chosen not to write it as:

s = “#{sleep 10; nil}string”

Of course, you could say that doing it that second way would be dumb  
and so on. But if you happen to know that =~ is faster than  
Regexp#match, or that a block is faster than Symbol#to\_proc, then the  
choice of the faster one becomes kind of instantaneous. (Unless of  
course you have some specific reason to choose the other, like wanting  
the MatchData object or wanting to squeeze your code down.)

In other words, we’re always making these choices, and if the way you  
choose happens to run faster, that’s not necessarily the root of any  
evil 🙂

> > Even then, I suspect that &: would still be slower,  
> > again, because it inserts another (unnecessary) method call. In the  
> > case of a truly dynamic method invocation, you would probably just use  
> > Object.send, but with &:, you still have to do the call to  
> > Symbol.to\_proc before that.
> 
> I can’t see a problem with making &: into a single operator. Thats how  
> constructs like x[0] ||= 0 work - the entire sequence from the [ to the = goes  
> into an operator, with x, 0, and 0 as arguments.

The &obj thing is more general, though. I guess it could be  
special-cased for symbols, though then that means weird things like  
that you wouldn’t be able to override #to\_proc for a symbol (which  
might not matter often but isn’t ideal).

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 29, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/14 "2008-07-29T18:19:59Z")

</div>

Hi –

On Tue, 29 Jul 2008, Wes G. wrote:

> Phlip wrote:
> 
> > Do you have a guess why? It seems both expressions could resolve to the  
> > same  
> > opcodes…
> 
> Isn’t it as simply as two more method calls in the &:to\_i case? One  
> call to Symbol.to\_proc and another call to Object.send, in addition to  
> the actual method call that you want.

I believe that’s right (though I haven’t dug into it). Basically, & is  
now a unary operator that calls #to\_proc on any object that implements  
it:

david-blacks-macbook:~ dblack$ irb19 --simple-prompt

> > obj = Object.new  
> > =\> #Object:0x3b94f8  
> > def obj.to\_proc; Proc.new { puts “Hi!” }; end  
> > =\> nil  
> > [1].each(&obj)  
> > Hi!  
> > =\> [1]

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 16, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/15 "2008-11-16T17:19:12Z")

</div>

Enrico Thierbach wrote:

> Besides, I found a hack that would decrease the runtime overhead of  
> using Symbol#to\_proc, which is to cache the Proc object inside the  
> symbol:
> 
> class Symbol  
> def to\_proc  
> @to\_proc ||= Proc.new { |\*args| args.shift. **send** (self, \*args) }  
> end  
> end
> 
> With this I get runtimes only two times the original ones. But I still  
> don’t know if I should consider that one safe. Do you have any idea on  
> that?

I think it’s a good optimization. I still don’t like the idea of  
slowing down stuff any more than necessary, no matter how you do it.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 16, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/16 "2008-11-16T14:46:07Z")

</div>

Wes G. wrote:

> All,
> 
> I was recently introduced to the Symbol.to\_proc trick where you can  
> invoke methods on collection elements in a map call with less syntax, as  
> in:
> 
> [“1”, “2”, “3”].map(&:to\_i)
> 
> While this is very clever looking (albeit potentially harder to read for  
> Ruby/Rails noobs, which implies a human performance cost), I started  
> thinking about it and decided this had to be slower than calling methods  
> directly, as in:
> 
> [“1”, “2”, “3”].map {|x| x.to\_i}
> 
> So I decided to benchmark it to see. My benchmark test shows that  
> calling the method through the Symbol.to\_proc method (at least in this  
> case) is 8.5X slower than doing the more readable method invocation.
> 
> My benchmark code is below. Is it a valid test? And if so, why should  
> we use Symbol.to\_proc unnecessarily when it is such a poor performer?
> 
> Thanks,  
> Wes
> 
> ==========benchmark code============
> 
> #!/usr/bin/ruby
> 
> require ‘benchmark’  
> include Benchmark
> 
> LOOP\_COUNT = 1\_000\_000  
> x = [“1”,“2”,“3”]  
> bmbm do |test|  
> test.report(“Method invoke”) do  
> LOOP\_COUNT.times {x.map {|elem| elem.to\_i}}  
> end
> 
> test.report(“Symbol.to\_proc”) do  
> LOOP\_COUNT.times {x.map(&:to\_i)}  
> end  
> end  
> ~

Hey wes,

you are testing “invoking explicit blocks” vs “creating a temporary Proc  
object and invoking via that”. What is missing here would be “create a  
proc once and invoke via that.” This would be the more real-life  
approach, because you usually invoke a Proc/block/whatever on a  
structure with more than three entries.

Besides, I found a hack that would decrease the runtime overhead of  
using Symbol#to\_proc, which is to cache the Proc object inside the  
symbol:

class Symbol  
def to\_proc  
@to\_proc ||= Proc.new { |\*args| args.shift. **send** (self, \*args) }  
end  
end

With this I get runtimes only two times the original ones. But I still  
don’t know if I should consider that one safe. Do you have any idea on  
that?

/eno

I have a post on my blog that goes into that in more detail:

> **[0x0a: Some optimization hacks…](https://1rad.wordpress.com/2008/11/10/0x0a-some-optimization-hacks/)**
>
> We are working on a somewhat larger Rails application, and one of the actions renders a table with up to 9.000 table elements. Now, somewhere when rendering that table we use a line like some\_array…

---

<div class="post-metadata">

**Author:** ![wesgamble](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/wesgamble/32/2752_2.png) [@wesgamble](https://www.ruby-forum.com/u/wesgamble)\
**Post date:** [May 27, 2021, 7:15pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/17 "2021-05-27T19:15:53Z")

</div>

Nearing 13 years later, I’m happy to report that this same benchmark scripts shows that Symbol.to\_proc is now slightly faster than using an explicit block in Ruby 2.6.6

---

<div class="post-metadata">

**Author:** ![jgomo3](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/jgomo3/32/950_2.png) [@jgomo3](https://www.ruby-forum.com/u/jgomo3)\
**Post date:** [June 5, 2021, 1:17pm UTC](https://www.ruby-forum.com/t/symbol-to-proc-is-way-slower-than-invoking-methods-directly/144365/18 "2021-06-05T13:17:40Z")

</div>

I was going to point out exactly something you mentioned. For more than 3 elements, the penalty is meaningless.

I bet there will be orders of magnitude less than 1MM calls to map, and biggest collections to process with it.

Anyways, I ran the test now (2021) Ruby 2.7 and the difference is minimal:

```auto
Rehearsal --------------------------------------------------
Method invoke 0.312939 0.000336 0.313275 ( 0.313942)
Symbol.to_proc 0.396422 0.000000 0.396422 ( 0.396948)
----------------------------------------- total: 0.709697sec

                     user system total real
Method invoke 0.312207 0.000000 0.312207 ( 0.312541)
Symbol.to_proc 0.389956 0.000000 0.389956 ( 0.390487)

```
