Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created Apr 29, 2011
Embed
What would you like to do?
Optimize Lookups for ruby-talk 382374
require 'benchmark'
REP = 10
Benchmark.bm 20 do |x|
pixels = Array.new(258000, rand(256)).freeze
old_lv = nil
x.report "old prep" do
# Set up example arrays to test the algorithm:
REP.times do
old_lv = Array.new
old_lv << Array.new(256, 0)
old_lv << Array.new(256, 1)
old_lv << Array.new(256, 2)
end
end
x.report "old" do
REP.times do
rgb = Array.new(pixels.length*3)
# The PALETTE transformation algorithm:
pixels.each_index do |i|
rgb[i*3] = old_lv[0][pixels[i]]
rgb[(i*3)+1] = old_lv[1][pixels[i]]
rgb[(i*3)+2] = old_lv[2][pixels[i]]
end
end
end
new_lv = nil
x.report "new prep" do
REP.times do
# Set up example arrays to test the algorithm:
new_lv = Array.new(256) { [0,1,2] }
end
end
x.report "new" do
REP.times do
rgb = Array.new(pixels.length*3)
# The PALETTE transformation algorithm:
pixels.each_with_index do |pix, i|
rgb[i * 3, 3] = new_lv[pix]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment