Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Add Comment
Please, Sign In to add comment