From: Alex Fenton Date: 2009-02-09T07:09:15+09:00 Subject: Re: calling random Boris Schmid wrote: > Am I correct if I remember that using send is quite expensive in > cpu-time? Especially when I am using it millions of times.. $pop is > about 5000 hosts large. > > 10000.times { > # Events to happen to every host this 0.1 year: > ["birth","death","infect","evolve"].shuffle.each {|event| Using #send with a String argument is slower than using it with a Symbol. Benchmark: ------- require 'benchmark' class Foo def bar; end end a_foo = Foo.new TIMES = 1_000_000 puts "Direct", Benchmark::measure { TIMES.times { a_foo.bar } } puts "Symbol", Benchmark::measure { TIMES.times { a_foo.send(:bar) } puts "String", Benchmark::measure { TIMES.times { a_foo.send("bar") } } ------- With Ruby 1.8: Direct 0.290000 0.000000 0.290000 ( 0.294786) Symbol 0.380000 0.000000 0.380000 ( 0.385319) String 0.550000 0.000000 0.550000 ( 0.556780) With Ruby 1.9, the difference between #send(a_symbol) and calling the method directly is negligible: Direct 0.210000 0.000000 0.210000 ( 0.209512) Symbol 0.210000 0.000000 0.210000 ( 0.210357) String 0.490000 0.000000 0.490000 ( 0.500568) alex