Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3689

Need for speed - array operations

From Chris Lervag <chris.lervag@gmail.com>
Newsgroups comp.lang.ruby
Subject Need for speed - array operations
Date 2011-04-29 03:54 -0500
Organization Service de news de lacave.net
Message-ID <2c5b9893b3d3fa487c6b9737df7c8df2@ruby-forum.com> (permalink)

Show all headers | View raw


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 = Array.new
lookup_values << Array.new(256, 0)
lookup_values << Array.new(256, 1)
lookup_values << Array.new(256, 2)
pixels = Array.new(258000, rand(256))
rgb = Array.new(pixels.length*3)

# The PALETTE transformation algorithm:
pixels.each_index do |i|
  rgb[i*3] = lookup_values[0][pixels[i]]
  rgb[(i*3)+1] = lookup_values[1][pixels[i]]
  rgb[(i*3)+2] = lookup_values[2][pixels[i]]
end

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Need for speed - array operations Chris Lervag <chris.lervag@gmail.com> - 2011-04-29 03:54 -0500
  Re: Need for speed - array operations Ralf Mueller <ralf.mueller@zmaw.de> - 2011-04-29 06:05 -0500
    Re: Need for speed - array operations Robert Klemme <shortcutter@googlemail.com> - 2011-04-29 06:23 -0500
  Re: Need for speed - array operations Chris Lervag <chris.lervag@gmail.com> - 2011-04-29 09:05 -0500
  Re: Need for speed - array operations Chris Lervag <chris.lervag@gmail.com> - 2011-04-29 16:45 -0500
  Re: Need for speed - array operations Pirogov Eugene <iamexile@gmail.com> - 2011-05-17 15:26 -0500
    Re: Need for speed - array operations Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-05-17 16:23 -0500

csiph-web