From: Eric Hodel Date: 2006-05-23T16:20:06+09:00 Subject: Re: Performance characteristics of Ruby constructs. On May 22, 2006, at 10:13 PM, Ola Bini wrote: > I'm right in the situation where I would like to find out some > performance characteristics for various parts of Ruby, to optimize an > application. Is there any resources available for this? The built-in profiler, Shugo's Prof and ZenHacks' zenprofile are all great for this. Oh, I almost forgot benchmark.rb, also built-in. > To make it concrete, let's take two examples. I'll skip straight to the second example. > Next example: there's fairly heave use of Class#=== throughout the > application, and the profiler says I spend much time there. Since you already know how to use the profiler you should be able to iteratively test small changes to make optimize either problem correctly. Propose a change, make it, test its effectiveness, revert if it doesn't help, repeat. > It's a fairly small class of Class comparisons I'm doing, though, > so maybe it would get me better performance by polluting the global > namespace like this: > > class Object > def __is_directive_token?; false; end > def __is_sequence_start_token?; false; end > end > > class Token > end > > class DirectiveToken > def __is_directive_token?; true; end > end > > and just do tok.__is_directive_token? instead of DirectiveToken === > tok. (This machine has a 266MHz Geode CPU, so forgive the poor numbers.) $ cat bm.rb require 'benchmark' class Object def directive_token? false end end class DirectiveToken def directive_token? true end end o = Object.new dt = DirectiveToken.new n = 1_000_000 Benchmark.bmbm do |bm| bm.report '#diretive_token?' do n.times { o.directive_token?; dt.directive_token? } end bm.report '#===' do n.times { DirectiveToken === o; DirectiveToken === dt } end end $ ruby -v bm.rb ruby 1.8.2 (2004-12-25) [i386-freebsd5] Rehearsal ---------------------------------------------------- #diretive_token? 15.226562 0.156250 15.382812 ( 16.111389) #=== 18.054688 0.117188 18.171875 ( 19.082567) ------------------------------------------ total: 33.554688sec user system total real #diretive_token? 15.625000 0.125000 15.750000 ( 16.463518) #=== 17.617188 0.171875 17.789062 ( 18.649016) > Any thoughts in this? Is there any place to find general > information this, or I'm out in the wild right now? My biggest question is: Why do you need to do all those class comparisons? Perhaps you can change your design to increase speed. Also, if small enough, posting your code may get the local parser geeks proposing all sorts of tuning tips. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com