Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Created February 6, 2018 05:38
Show Gist options
  • Save yabberyabber/6e6574ae9ed7a305a0b904a503c3ed56 to your computer and use it in GitHub Desktop.
Save yabberyabber/6e6574ae9ed7a305a0b904a503c3ed56 to your computer and use it in GitHub Desktop.
import random
import time
class Item():
def __init__(self, x, y, z, width, height, depth):
self.x = x
self.y = y
self.z = z
self.width = width
self.height = height
self.depth = depth
def intersects(self, other):
if self.x + self.width < other.x:
return False
if other.x + other.width < self.x:
return False
if self.y + self.height < other.y:
return False
if other.y + other.height < self.y:
return False
if self.z + self.depth < other.z:
return False
if other.z + other.depth < self.z:
return False
return True
# Make this class faster. I don't care how you do it. There's some low hanging fruit
# and that's fine. Just do something to speed up the find_items_at_point.
class Space():
def __init__(self):
self.items = []
def insert(self, item):
self.items.append(item)
def find_items_at_point(self, x, y, z):
point = Item(x, y, z, 0, 0, 0)
ret = []
for item in self.items:
if item.intersects(point):
ret.append(item)
return ret
def simulate(insertions, queries):
def random_item():
return Item(100 * random.random(),
100 * random.random(),
100 * random.random(),
random.random(),
random.random(),
random.random())
space = Space()
for _ in range(insertions):
space.insert(random_item())
for _ in range(queries):
space.find_items_at_point(100 * random.random(),
100 * random.random(),
100 * random.random())
start = time.time()
simulate(10000, 1000)
end = time.time()
print(end - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment