Hi -- On Mon, 9 Feb 2009, Alex Fenton wrote: > 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) However, calling #intern/to_sym is (slightly) slower than just sending a string. This is Ruby 1.9.1: Direct 0.150000 0.000000 0.150000 ( 0.157290) Symbol 0.170000 0.000000 0.170000 ( 0.166353) String 0.470000 0.000000 0.470000 ( 0.476854) #intern 0.520000 0.020000 0.540000 ( 0.537574) So if you've got a string already, it's probably not worth doing the conversion explicitly. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!