f8g

ブロックもっと速くならないか

ループの中でブロックを使うと明らかに遅かったので測ってみた。

# Bitmapにイテレータ
class System::Drawing::Bitmap
	def each
		x = 0
		while x < self.width
			y = 0
			while y < self.height
				yield GetPixel(x, y)
				y += 1
			end
			x += 1
		end
	end
end

画像のサイズは500 * 468。GetPixelを呼ぶ。

sw = System::Diagnostics::Stopwatch.new

# イテレータ
sw.Start
image.each {|pixel|
	pixel
}
sw.Stop
t1 = 0.001 * sw.ElapsedMilliseconds

# 普通の
sw.Start
x = 0
while x < image.width
	y = 0
	while y < image.height
		image.GetPixel(x, y)
		y += 1
	end
	x += 1
end
sw.Stop
t2 = 0.001 * sw.ElapsedMilliseconds

p t1, t2 - t1
# 結果
# 8.962
# 0.863

10倍ぐらい遅くて、このままだと使い物にならない。どうすれば速くなるんだろ。