From: Tom Gilbert Date: 2002-11-21T23:25:27+09:00 Subject: Re: Ruby is too slow * MetalOne (jcb@iteris.com) wrote: > while (imageIt != last) > { > *imageIt = lookup[*imageIt]; > imageIt++; > } Simple loop and 3 pointer dereferences. > for (int i=0; i < size; ++i) > { > image[i] = lookup[image[i]]; > } Simple loop and 3 array lookups. > size.times { |i| > image[i] = lookup[image[i]] > } One function call per pixel for the loop and 3 function calls for the lookups. I believe that accounts for the difference you are seeing. You have to remember that .times is implemented using a block and then yield()ing it that many times. My advice would be to break this code out into a module. I wrote an app that did ebook processing, and was using stuff like .each for each line of the book and then .each for each character on the line, and the function call overhead was insane. One I broke the per character stuff into a C module and called it on each line as a ruby function, the speed improved by a factor of 50. Tom. -- .^. .-------------------------------------------------------. /V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk | /( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk | ^^-^^ `-------------------------------------------------------'