def test_unpickle_list(loops, timer, pickle, options): pickled_list = pickle.dumps(LIST, options.protocol) # Warm-up runs. pickle.loads(pickled_list) pickle.loads(pickled_list) loops = loops // 5 # Scale to compensate for the workload. times = [] for _ in xrange(options.num_runs): t0 = timer() for _ in xrange(loops): pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) pickle.loads(pickled_list) t1 = timer() times.append(t1 - t0) return times
def print_pos(pos, output): hex = pos.hex done = pos.done size = hex.size for y in xrange(size): print(u_lit(" ") * (size - y - 1), end=u_lit(""), file=output) for x in xrange(size + y): pos2 = (x, y) id = hex.get_by_pos(pos2).id if done.already_done(id): c = unicode(done[id][0]) if done[id][0] != EMPTY else u_lit(".") else: c = u_lit("?") print(u_lit("%s ") % c, end=u_lit(""), file=output) print(end=u_lit("\n"), file=output) for y in xrange(1, size): print(u_lit(" ") * y, end=u_lit(""), file=output) for x in xrange(y, size * 2 - 1): ry = size + y - 1 pos2 = (x, ry) id = hex.get_by_pos(pos2).id if done.already_done(id): c = unicode(done[id][0]) if done[id][0] != EMPTY else u_lit(".") else: c = u_lit("?") print(u_lit("%s ") % c, end=u_lit(""), file=output) print(end=u_lit("\n"), file=output)
def print_pos(pos, output): hex = pos.hex done = pos.done size = hex.size for y in xrange(size): print(u_lit(" ") * (size - y - 1), end=u_lit(""), file=output) for x in xrange(size + y): pos2 = (x, y) id = hex.get_by_pos(pos2).id if done.already_done(id): c = unicode( done[id][0]) if done[id][0] != EMPTY else u_lit(".") else: c = u_lit("?") print(u_lit("%s ") % c, end=u_lit(""), file=output) print(end=u_lit("\n"), file=output) for y in xrange(1, size): print(u_lit(" ") * y, end=u_lit(""), file=output) for x in xrange(y, size * 2 - 1): ry = size + y - 1 pos2 = (x, ry) id = hex.get_by_pos(pos2).id if done.already_done(id): c = unicode( done[id][0]) if done[id][0] != EMPTY else u_lit(".") else: c = u_lit("?") print(u_lit("%s ") % c, end=u_lit(""), file=output) print(end=u_lit("\n"), file=output)
def print_board(board, w=w, h=h): for y in xrange(h): for x in xrange(w): print(board[x + y * w]) print('') if y % 2 == 0: print('') print()
def get_footprints(board, cti, pieces): fps = [[[] for p in xrange(len(pieces))] for ci in xrange(len(board))] for c in board: for pi, p in enumerate(pieces): for pp in p: fp = frozenset([cti[c + o] for o in pp if (c + o) in cti]) if len(fp) == 5: fps[min(fp)][pi].append(fp) return fps
def get_footprints(board:List(float), cti:Dict(float,int), pieces:List(List(List(float))))-> \ List(List(List(Set(List(int))))): fps = [[[] for p in xrange(len(pieces))] for ci in xrange(len(board))] for c in board: for pi, p in enumerate(pieces): for pp in p: fp = frozenset([cti[c + o] for o in pp if (c + o) in cti]) if len(fp) == 5: fps[min(fp)][pi].append(fp) return fps
def main(n, timer): l = [] for i in xrange(n): t0 = timer() for case in cases: data, count = globals()[case] for i in xrange(count): json.dumps(data) l.append(timer() - t0) return l
def main(): client = AsyncHTTPClient() for i in xrange(count): t0 = timer() futures = [client.fetch(url) for j in xrange(CONCURRENCY)] for fut in futures: resp = yield fut buf = resp.buffer buf.seek(0, 2) assert buf.tell() == len(CHUNK) * NCHUNKS t1 = timer() times.append(t1 - t0)
def test_iterative_count(iterations, timer, num_threads): # Warm up. count(1000) times = [] for _ in xrange(iterations): t0 = timer() for _ in xrange(num_threads): count() t1 = timer() times.append(t1 - t0) return times
def main(n, timer): times = [] for i in xrange(n): t0 = timer() free = frozenset(xrange(len(board))) curr_board = [-1] * len(board) pieces_left = list(range(len(pieces))) solutions = [] solve(SOLVE_ARG, 0, free, curr_board, pieces_left, solutions) #print len(solutions), 'solutions found\n' #for i in (0, -1): print_board(solutions[i]) tk = timer() times.append(tk - t0) return times
def test_mako(count, timer): table = [xrange(150) for _ in xrange(150)] # Warm up Mako. MAKO_TMPL.render(table=table) MAKO_TMPL.render(table=table) times = [] for _ in xrange(count): t0 = timer() MAKO_TMPL.render(table=table) t1 = timer() times.append(t1 - t0) return times
def test_mako(count, timer): table = [xrange(150) for _ in xrange(150)] # Warm up Mako. MAKO_TMPL.render(table = table) MAKO_TMPL.render(table = table) times = [] for _ in xrange(count): t0 = timer() MAKO_TMPL.render(table = table) t1 = timer() times.append(t1 - t0) return times
def test_threaded_count(iterations, timer, num_threads): # Warm up. count(1000) times = [] for _ in xrange(iterations): threads = [threading.Thread(target=count) for _ in xrange(num_threads)] t0 = timer() for thread in threads: thread.start() for thread in threads: thread.join() t1 = timer() times.append(t1 - t0) return times
def test_django(count, timer): table = [xrange(150) for _ in xrange(150)] context = Context({"table": table}) # Warm up Django. DJANGO_TMPL.render(context) DJANGO_TMPL.render(context) times = [] for _ in xrange(count): t0 = timer() data = DJANGO_TMPL.render(context) t1 = timer() times.append(t1 - t0) return times
def test_qsize(self): if sys.version_info[:2] > (2, 6): super(UniQueueTest, self).test_qsize() else: QueueTest.test_qsize(self) self.obj.clear() s = random.randint(1, 32) n = random.randint(2, 5) for _ in xrange(n): for i in xrange(s): self.obj.put(i) self.assertEqual(self.obj.qsize(), s)
def remove_unfixed(self, v): changed = False for i in xrange(self.count): if not self.already_done(i): if self.remove(i, v): changed = True return changed
def benchmark(n): points = [None] * n for i in xrange(n): points[i] = Point(i) for p in points: p.normalize() return maximize(points)
def run_etree_benchmark(iterations, timer, etree, bench_func): times = [] xml_root = build_xml_tree(etree) xml_data = etree.tostring(xml_root) # not using NamedTemporaryFile() here as re-opening it is not portable tf, file_path = tempfile.mkstemp() try: etree.ElementTree(xml_root).write(file_path) # warm up bench_func(etree, file_path, xml_data, xml_root) bench_func(etree, file_path, xml_data, xml_root) for _ in xrange(iterations): t0 = timer() bench_func(etree, file_path, xml_data, xml_root) t1 = timer() times.append(t1 - t0) finally: try: os.close(tf) except EnvironmentError: pass try: os.unlink(file_path) except EnvironmentError: pass return times
def test_calls(iterations, timer): times = [] f = Foo() if hasattr(f, '__dict__'): raise Exception("f has a __dict__ attribute!") for _ in xrange(iterations): t0 = timer() # 20 calls f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) t1 = timer() times.append(t1 - t0) return times
def test_calls(iterations, timer): times = [] a = Foo() b = Bar() c = Baz() for _ in xrange(iterations): t0 = timer() # 18 calls a.foo(b, c) b.foo(c, a) c.foo(a, b) a.foo(b, c) b.foo(c, a) c.foo(a, b) a.foo(b, c) b.foo(c, a) c.foo(a, b) a.foo(b, c) b.foo(c, a) c.foo(a, b) a.foo(b, c) b.foo(c, a) c.foo(a, b) a.foo(b, c) b.foo(c, a) c.foo(a, b) t1 = timer() times.append(t1 - t0) return times
def test_calls(iterations, timer): times = [] f = Foo() for _ in xrange(iterations): t0 = timer() # 20 calls f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) f.foo(1, 2, 3, 4) t1 = timer() times.append(t1 - t0) return times
def run(self, iterations:int)->bool: for i in xrange(iterations): taskWorkArea.holdCount = 0 taskWorkArea.qpktCount = 0 IdleTask(I_IDLE, 1, 10000, TaskState().running(), IdleTaskRec()) wkq = Packet(None, 0, K_WORK) wkq = Packet(wkq , 0, K_WORK) WorkTask(I_WORK, 1000, wkq, TaskState().waitingWithPacket(), WorkerTaskRec()) wkq = Packet(None, I_DEVA, K_DEV) wkq = Packet(wkq , I_DEVA, K_DEV) wkq = Packet(wkq , I_DEVA, K_DEV) HandlerTask(I_HANDLERA, 2000, wkq, TaskState().waitingWithPacket(), HandlerTaskRec()) wkq = Packet(None, I_DEVB, K_DEV) wkq = Packet(wkq , I_DEVB, K_DEV) wkq = Packet(wkq , I_DEVB, K_DEV) HandlerTask(I_HANDLERB, 3000, wkq, TaskState().waitingWithPacket(), HandlerTaskRec()) wkq = None; DeviceTask(I_DEVA, 4000, wkq, TaskState().waiting(), DeviceTaskRec()); DeviceTask(I_DEVB, 5000, wkq, TaskState().waiting(), DeviceTaskRec()); schedule() if taskWorkArea.holdCount == 9297 and taskWorkArea.qpktCount == 23246: pass else: return False return True
def test_calls(iterations, timer): times = [] for _ in xrange(iterations): t0 = timer() # 20 calls foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) foo(1, 2, 3, 4) t1 = timer() times.append(t1 - t0) return times
def next_cell_max_choice(self): maxlen = 1 maxi = -1 for i in xrange(self.count): if maxlen < len(self.cells[i]): maxlen = len(self.cells[i]) maxi = i return maxi
def test_len(self): self.assertEqual(len(self.obj), 0) s = random.randint(1, 32) for i in xrange(s): self.obj.put(i) self.assertEqual(len(self.obj), s)
def test_qsize(self): self.assertEqual(self.obj.qsize(), 0) s = random.randint(1, 32) for i in xrange(s): self.obj.put(i) self.assertEqual(self.obj.qsize(), s)
def read_file(file): lines = [line.strip("\r\n") for line in file.splitlines()] size = int(lines[0]) hex = Hex(size) linei = 1 tiles = 8 * [0] done = Done(hex.count) for y in xrange(size): line = lines[linei][size - y - 1:] p = 0 for x in xrange(size + y): tile = line[p:p + 2]; p += 2 if tile[1] == ".": inctile = EMPTY else: inctile = int(tile) tiles[inctile] += 1 # Look for locked tiles if tile[0] == "+": print("Adding locked tile: %d at pos %d, %d, id=%d" % (inctile, x, y, hex.get_by_pos((x, y)).id)) done.set_done(hex.get_by_pos((x, y)).id, inctile) linei += 1 for y in xrange(1, size): ry = size - 1 + y line = lines[linei][y:] p = 0 for x in xrange(y, size * 2 - 1): tile = line[p:p + 2]; p += 2 if tile[1] == ".": inctile = EMPTY else: inctile = int(tile) tiles[inctile] += 1 # Look for locked tiles if tile[0] == "+": print("Adding locked tile: %d at pos %d, %d, id=%d" % (inctile, x, ry, hex.get_by_pos((x, ry)).id)) done.set_done(hex.get_by_pos((x, ry)).id, inctile) linei += 1 hex.link_nodes() done.filter_tiles(tiles) return Pos(hex, tiles, done)
def combinations(l:List((List(float), List(float), float)))->List((List((List(float), List(float), float)), (List(float), List(float), float))): #<- ret ty temp """Pure-Python implementation of itertools.combinations(l, 2).""" result = [] for x in xrange(len(l) - 1): ls = l[x+1:] for y in ls: result.append((l[x],y)) return result
def main(n, timer): times = [] for i in xrange(n): t0 = timer() fannkuch(DEFAULT_ARG) tk = timer() times.append(tk - t0) return times
def next_cell_min_choice(self): minlen = 10 mini = -1 for i in xrange(self.count): if 1 < len(self.cells[i]) < minlen: minlen = len(self.cells[i]) mini = i return mini
def test_put(self): a = random.randint(1, 32) n = random.randint(1, 5) for _ in xrange(n): self.obj.put(a) v = self.obj.get() self.assertEqual(v, (unicode(a), n))
def read_file(file): lines = [line.strip("\r\n") for line in file.splitlines()] size = int(lines[0]) hex = Hex(size) linei = 1 tiles = 8 * [0] done = Done(hex.count) for y in xrange(size): line = lines[linei][size - y - 1:] p = 0 for x in xrange(size + y): tile = line[p:p + 2] p += 2 if tile[1] == ".": inctile = EMPTY else: inctile = int(tile) tiles[inctile] += 1 # Look for locked tiles if tile[0] == "+": print("Adding locked tile: %d at pos %d, %d, id=%d" % (inctile, x, y, hex.get_by_pos((x, y)).id)) done.set_done(hex.get_by_pos((x, y)).id, inctile) linei += 1 for y in xrange(1, size): ry = size - 1 + y line = lines[linei][y:] p = 0 for x in xrange(y, size * 2 - 1): tile = line[p:p + 2] p += 2 if tile[1] == ".": inctile = EMPTY else: inctile = int(tile) tiles[inctile] += 1 # Look for locked tiles if tile[0] == "+": print("Adding locked tile: %d at pos %d, %d, id=%d" % (inctile, x, ry, hex.get_by_pos((x, ry)).id)) done.set_done(hex.get_by_pos((x, ry)).id, inctile) linei += 1 hex.link_nodes() done.filter_tiles(tiles) return Pos(hex, tiles, done)
def combinations(l): """Pure-Python implementation of itertools.combinations(l, 2).""" result = [] for x in xrange(len(l) - 1): ls = l[x+1:] for y in ls: result.append((l[x],y)) return result
def test_pickle_dict(loops, timer, pickle, options): # Warm-up runs. pickle.dumps(MICRO_DICT, options.protocol) pickle.dumps(MICRO_DICT, options.protocol) loops = max(1, loops // 10) times = [] for _ in xrange(options.num_runs): t0 = timer() for _ in xrange(loops): pickle.dumps(MICRO_DICT, options.protocol) pickle.dumps(MICRO_DICT, options.protocol) pickle.dumps(MICRO_DICT, options.protocol) pickle.dumps(MICRO_DICT, options.protocol) pickle.dumps(MICRO_DICT, options.protocol) t1 = timer() times.append(t1 - t0) return times
def get_puzzle(w=w, h=h): board = [E*x + S*y + (y%2) for y in xrange(h) for x in xrange(w)] cti = dict((board[i], i) for i in xrange(len(board))) idos = [[E, E, E, SE], # incremental direction offsets [SE, SW, W, SW], [W, W, SW, SE], [E, E, SW, SE], [NW, W, NW, SE, SW], [E, E, NE, W], [NW, NE, NE, W], [NE, SE, E, NE], [SE, SE, E, SE], [E, NW, NW, NW]] perms = (permute(p, idos[3]) for p in idos) # restrict piece 4 pieces = [[convert(pp) for pp in p] for p in perms] return (board, cti, pieces)
def permute(ido, r_ido, rotate=rotate, flip=flip): ps = [ido] for r in xrange(dir_no - 1): ps.append(rotate(ps[-1])) if ido == r_ido: # C2-symmetry ps = ps[0:dir_no // 2] for pp in ps[:]: ps.append(flip(pp)) return ps
def main(n, timer): times = [] for i in xrange(n): t0 = timer() u = [1] * DEFAULT_N for dummy in xrange(10): v = eval_AtA_times_u(u) u = eval_AtA_times_u(v) vBv = vv = 0 for ue, ve in izip(u, v): vBv += ue * ve vv += ve * ve tk = timer() times.append(tk - t0) return times
def next_cell_highest_value(self): maxval = -1 maxi = -1 for i in xrange(self.count): if (not self.already_done(i)): maxvali = max(k for k in self.cells[i] if k != EMPTY) if maxval < maxvali: maxval = maxvali maxi = i return maxi
def main(n, timer): # only run 1/25th of the requested number of iterations. # with the default n=50 from runner.py, this means twice. l = [] for i in xrange(n): t0 = timer() run_level36() time_elapsed = timer() - t0 l.append(time_elapsed) return l
def test_simple_output(iterations, timer, logger): times = [] m = MESSAGE for _ in xrange(iterations): t0 = timer() for _ in xrange(1000): logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) logger.warn(m) t1 = timer() times.append(t1 - t0) return times
def main(arg, timer): # XXX warmup times = [] for i in xrange(arg): t0 = timer() o = benchmark(POINTS) tk = timer() times.append(tk - t0) return times
def test_formatted_output(iterations, timer, logger): times = [] f = FORMAT m = MESSAGE for _ in xrange(iterations): t0 = timer() for _ in xrange(1000): logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) logger.warn(f, m) t1 = timer() times.append(t1 - t0) return times
def get_puzzle(w=w, h=h): board = [E * x + S * y + (y % 2) for y in xrange(h) for x in xrange(w)] cti = dict((board[i], i) for i in xrange(len(board))) idos = [ [E, E, E, SE], # incremental direction offsets [SE, SW, W, SW], [W, W, SW, SE], [E, E, SW, SE], [NW, W, NW, SE, SW], [E, E, NE, W], [NW, NE, NE, W], [NE, SE, E, NE], [SE, SE, E, SE], [E, NW, NW, NW] ] perms = (permute(p, idos[3]) for p in idos) # restrict piece 4 pieces = [[convert(pp) for pp in p] for p in perms] return (board, cti, pieces)
def test_n_queens(iterations, timer): # Warm-up runs. list(n_queens(8)) list(n_queens(8)) times = [] for _ in xrange(iterations): t0 = timer() list(n_queens(8)) t1 = timer() times.append(t1 - t0) return times