On Fri, Apr 29, 2011 at 1:05 PM, Ralf Mueller <ralf.mueller / zmaw.de> wrote: > On 04/29/2011 10:54 AM, Chris Lervag wrote: >> >> Hi, >> >> Im working on a library to decode medical image files, and some of these >> files are encoded as 'PALETTE COLOR', which means you have a lookup >> table for red, green and blue pixel values. The final RGB pixel array is >> constructed by hitting the lookup table with the original pixel values. >> In my implementation I am using an iterator, and it is kinda slow. I >> cant spot an obvious way to improve on it though, so I thought I'd put >> the question out here and see if any of you more experienced Rubyists >> can suggest a more efficient way of doing this. >> >> Thanks, >> Chris >> >> Example code: >> # Set up example arrays to test the algorithm: >> lookup_values =3D Array.new >> lookup_values<< =A0Array.new(256, 0) >> lookup_values<< =A0Array.new(256, 1) >> lookup_values<< =A0Array.new(256, 2) >> pixels =3D Array.new(258000, rand(256)) >> rgb =3D Array.new(pixels.length*3) >> >> # The PALETTE transformation algorithm: >> pixels.each_index do |i| >> =A0 rgb[i*3] =3D lookup_values[0][pixels[i]] >> =A0 rgb[(i*3)+1] =3D lookup_values[1][pixels[i]] >> =A0 rgb[(i*3)+2] =3D lookup_values[2][pixels[i]] >> end >> > You might try narray: http://narray.rubyforge.org/. It's available as a g= em. Not so fast. There is room for optimization even in this implementation. We can shave off over 12% with a pure Ruby solution: 13:18:20 Temp$ ruby19 lv.rb user system total real old prep 0.000000 0.000000 0.000000 ( 0.000000) old 1.812000 0.000000 1.812000 ( 1.806000) new prep 0.000000 0.000000 0.000000 ( 0.001000) new 1.016000 0.047000 1.063000 ( 1.060000) https://gist.github.com/948178 Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/