Created
July 18, 2011 18:28
-
-
Save bil-bas/1090250 to your computer and use it in GitHub Desktop.
Optmised collision
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
class Rect | |
def collide_new?(rect) | |
rect = rect.to_rect | |
rect.x < x + width and | |
x < rect.x + rect.width and | |
rect.y < y + height and | |
y < rect.y + rect.height | |
end | |
end | |
a, b = Rect.new(0, 0, 1, 1), Rect.new(2, 2, 1, 1) | |
Benchmark.bm do |x| | |
x.report("a.collide? b ") { 10000.times { a.collide? b } } | |
x.report("my a.collide? b") { 10000.times { a.collide_new? b }} | |
x.report("a.collide? a ") { 10000.times { a.collide? a }} | |
x.report("my a.collide? a") { 10000.times { a.collide_new? a }} | |
end | |
# user system total real | |
# a.collide? b 0.172000 0.000000 0.172000 ( 0.172000) | |
# my a.collide? b 0.016000 0.000000 0.016000 ( 0.013000) | |
# a.collide? a 0.093000 0.000000 0.093000 ( 0.089000) | |
# my a.collide? a 0.047000 0.000000 0.047000 ( 0.046000) | |
# Compared with the collide? in the gem (which fails in certain cases). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment