Advertisement
Guest User

python 3d data mining performance

a guest
Aug 29th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. import timeit
  2. from random import choice
  3.  
  4. grid_size = (999, 999, 99)
  5.  
  6. class Bug(object):
  7.   def __init__(self, id):
  8.     self.id = id
  9.     self.trace = []
  10.   def get_possibles_future_trace_point(self):
  11.     point =  self.trace[0]
  12.     possible_points = []
  13.    
  14.     possible_points.append((point[0]-1, point[1]+1, point[2]))
  15.     possible_points.append((point[0],   point[1]+1, point[2]))
  16.     possible_points.append((point[0]+1, point[1]+1, point[2]))
  17.     possible_points.append((point[0]-1, point[1],   point[2]))
  18.     possible_points.append((point[0]+1, point[1],   point[2]))
  19.     possible_points.append((point[0]-1, point[1]-1, point[2]))
  20.     possible_points.append((point[0],   point[1]-1, point[2]))
  21.     possible_points.append((point[0]+1, point[1]-1, point[2]))
  22.    
  23.     possible_points_oks = []
  24.     for possible_point in possible_points:
  25.       ok = True
  26.       if possible_point in self.trace:
  27.         ok = False
  28.       else:
  29.         for point in possible_point:
  30.           if point < 0:
  31.             ok = False
  32.         if ok:
  33.           possible_points_oks.append(possible_point)
  34.    
  35.     return possible_points_oks
  36.  
  37. def get_bugs(n):
  38.   bugs = []
  39.   for i in range(n):
  40.     bugs.append(Bug(i))
  41.   return bugs
  42.  
  43. def set_bugs_3d_random_trace(bugs, trace_length = 2):
  44.   for bug in bugs:
  45.     while len(bug.trace) <= trace_length:
  46.       bug.trace.append(get_bug_new_trace_point(bug))
  47.  
  48. def get_bug_new_trace_point(bug):
  49.   if not bug.trace:
  50.     return (choice(range(0, grid_size[0])), choice(range(0, grid_size[1])), choice(range(0, grid_size[2])))
  51.   return get_point_near_this_point(bug.trace[len(bug.trace)-1])
  52.  
  53. def get_point_near_this_point(point):
  54.   x = choice([-1, 0, 1])
  55.   y = choice([-1, 0, 1])
  56.   z = choice([-1, 0, 1])
  57.   new_point = (point[0] + x, point[1] + y, point[2] + z)
  58.   if (new_point[0] > 0 and new_point[0] <= grid_size[0]) and\
  59.      (new_point[1] > 0 and new_point[1] <= grid_size[1]) and\
  60.     (new_point[2] > 0 and new_point[2] <= grid_size[2]):
  61.     return new_point
  62.   return get_point_near_this_point(point)
  63.  
  64. def get_positioned_bugs(n):
  65.   bugs = get_bugs(n)
  66.   set_bugs_3d_random_trace(bugs)
  67.   return bugs
  68.  
  69. def get_str_map(bugs):
  70.   map = {}
  71.   for bug in bugs:
  72.     for trace_point in bug.trace:
  73.       map[str(trace_point[0])+'.'+str(trace_point[1])+'.'+str(trace_point[2])] = bug
  74.   return map
  75.  
  76. def get_int_map(bugs):
  77.   map = {}
  78.   for bug in bugs:
  79.     for trace_point in bug.trace:
  80.       key = trace_point[0]*100000+trace_point[1]*100+trace_point[2]
  81.       map[key] = bug
  82.   return map
  83.  
  84. def get_3d_map(bugs):
  85.   map = {}
  86.   for bug in bugs:
  87.     for trace_point in bug.trace:
  88.       x = trace_point[0]
  89.       y = trace_point[1]
  90.       z = trace_point[2]
  91.       if x not in map:
  92.         map[x] = {}
  93.       if y not in map[x]:
  94.         map[x][y] = {}
  95.       map[x][y][z] = bug
  96.   return map
  97.  
  98. def find_collisions_str_map(bugs, map_str):
  99.   collisions_bugs = []
  100.   for bug in bugs:
  101.     for trace_point in bug.get_possibles_future_trace_point():
  102.       #import pdb; pdb.set_trace()
  103.       key = str(trace_point[0])+'.'+str(trace_point[1])+'.'+str(trace_point[2])
  104.       if key in map_str:
  105.         if map_str[key].id != bug.id:
  106.           collisions_bugs.append(map_str[key])
  107.   return collisions_bugs
  108.  
  109.  
  110. def find_collisions_int_map(bugs, map_str):
  111.   collisions_bugs = []
  112.   for bug in bugs:
  113.     for trace_point in bug.get_possibles_future_trace_point():
  114.       key = trace_point[0]*1000000+trace_point[1]*1000+trace_point[2]
  115.       if key in map_str:
  116.         if map_str[key].id != bug.id:
  117.           collisions_bugs.append(map_str[key])
  118.   return collisions_bugs
  119.  
  120. def find_collisions_3d_map(bugs, map):
  121.   collisions_bugs = []
  122.   for bug in bugs:
  123.     for trace_point in bug.get_possibles_future_trace_point():
  124.       x = trace_point[0]
  125.       y = trace_point[1]
  126.       z = trace_point[2]
  127.       if x in map:
  128.         if y in map[x]:
  129.           if z in map[x][y]:
  130.             collisions_bugs.append(map[x][y][z])
  131.   return collisions_bugs
  132.  
  133. bugs = get_positioned_bugs(100000)
  134. map_str = get_str_map(bugs)
  135. map_int = get_int_map(bugs)
  136. map_3d = get_3d_map(bugs)
  137.  
  138. print('str', timeit.timeit('find_collisions_str_map(bugs, map_str)', number=10, setup='from __main__ import find_collisions_str_map, bugs, map_str'))
  139. print('int', timeit.timeit('find_collisions_int_map(bugs, map_int)', number=10, setup='from __main__ import find_collisions_int_map, bugs, map_int'))
  140. print('3d ', timeit.timeit('find_collisions_3d_map(bugs, map_3d)', number=10, setup='from __main__ import find_collisions_3d_map, bugs, map_3d'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement