Created
April 29, 2011 11:21
-
-
Save rklemme/948178 to your computer and use it in GitHub Desktop.
Optimize Lookups for ruby-talk 382374
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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