From: Mathieu Bouchard Date: 2001-05-06T03:34:47+09:00 Subject: [ruby-talk:14715] Re: Why's Ruby so slow in this case? On Sun, 6 May 2001, Stefan Matthias Aust wrote: > Sure, Ruby, being interpreted, is slower than a compiled language. > But I didn't expect that it is soo slow! From what I've seen of your code, you've always ran your code with the profiler on. The profiler slows down your code alot. It may even make execution time proportions different (just like in quantum physics, observing the phenomenon changes the phenomenon :-) > case d > when 0 In Ruby, cases _have_ to be tested sequentially, unlike C, which allows integers only, and can optimize situations like this using a static array of gotos. > def xytoh(x, y) > y * $mapsize + x > end > def htox(h) > h % $mapsize > end > def htoy(h) > h / $mapsize > end Those seem to be almost-unnecessary abstractions. I would be quite tempted to eliminate them. > but that doesn't really help. The problem IMHO is that all that tiny > method calls take their time and this sums up. For example, I don't > really understand why a simple method like > [...] > needs more than 1 ms per call! The measurement may not be accurate. Note that times() has to be called for every method call because of the profiler, and that it is a system call, and that system calls need context switches, and that context switches are usually, well, not fast. This makes calling/returning take an unusually large slice of the time pie. > | x y | > MapSize := 35. > x := y := 4. > Time millisecondsToRun: [ > 100000 timesRepeat: [ > x >= 0 and: [x < MapSize and: [y >= 0 and: [y < MapSize]]]]] > => 125 > I'd like to understand why Ruby is 100 times slower than the slowest > Smalltalk. VisualWorks Smalltalk needs just 20ms for 100.000 calls, > BTW. I translated that piece of code back into Ruby and compared with GNU SmallTalk. It's only about 4 times slower. Here is the code I used: require "benchmark" # gotoken's MapSize = 35 x = y = 4 Benchmark.bm(1) {|b| b.report("helo") { 100000.times { x >= 0 && x < MapSize && y >= 0 && y < MapSize }}} matju