def run(self): for p in range(self.N): if self.processed[p]: continue self.processed[p] = True self.order.append(p) seeds = FastRBTree() self._update(p, seeds) while seeds: # pop_min returns ( (reachability, q), q ). _, q = seeds.pop_min() self.processed[q] = True self.order.append(q) self._update(q, seeds) start = timer() self._extract_cluster_xi() print("extract cluster took", timer() - start)
#! /usr/bin/env python # coding:utf-8 from __future__ import division import heapq import bintrees import random if __name__ == '__main__': from benchmarker import Benchmarker from itertools import repeat, izip from bintrees import FastRBTree # initialize heapq h = range(10000) heapq.heapify(h) # initialize AVLTree m = izip(xrange(10000), repeat(True)) t = FastRBTree(m) for bm in Benchmarker(width=20, loop=100000, cycle=3, extra=1): for _ in bm.empty(): pass for _ in bm('heapq'): heapq.heappop(h) heapq.heappush(h, random.randint(-100000, 100000)) for _ in bm('FastRBTree'): t.pop_min() t[random.randint(-100000, 100000)] = True
class OrderQueue(object): """ """ def __init__(self): self.Orders = FastRBTree() self.totalAmount = 0.0 def append(self,order): k = order.timestamp self.Orders.insert(k,order) self.totalAmount += self.Orders[k].amount def count(self): return self.Orders.count def min_item(self): return self.Orders.min_item() def eat(self,amount): """ use this only if amount <= self.amount return OrderToPop,RestAmount """ current_amount = amount to_pop_orders = [] while self.totalAmount>0 and current_amount>0: if not self.Orders or self.Orders.count==0: #print "amount:%.4f" % self.totalAmount self.totalAmount = 0.0 return [] min_item = self.Orders.min_item() min_i = min_item[1] # 1.enough # 2.not enough if min_i.amount <= current_amount: current_amount -= min_i.amount self.totalAmount -= min_i.amount to_pop_orders.append(self.Orders.pop_min()[1]) continue # this is a hard case , need to take care of # first we split the order , take amount from current order # and make elif min_i.amount > current_amount: new_order = min_i.copy() new_order.amount = current_amount to_pop_orders.append(new_order) min_i.amount -= current_amount self.totalAmount -= current_amount current_amount = 0 break # return: # 1.orders # 2.amount to trade # 3.amount left return to_pop_orders def remove(self,order): k = order.timestamp self.totalAmount -= self.Orders[k].amount self.Orders.remove(k) def get_price_depth(self): return self.totalAmount def is_empty(self): return self.Orders.count == 0
class OrderQueue(object): """ """ def __init__(self): self.Orders = FastRBTree() self.totalAmount = 0.0 def append(self, order): k = order.timestamp self.Orders.insert(k, order) self.totalAmount += self.Orders[k].amount def count(self): return self.Orders.count def min_item(self): return self.Orders.min_item() def eat(self, amount): """ use this only if amount <= self.amount return OrderToPop,RestAmount """ current_amount = amount to_pop_orders = [] while self.totalAmount > 0 and current_amount > 0: if not self.Orders or self.Orders.count == 0: #print "amount:%.4f" % self.totalAmount self.totalAmount = 0.0 return [] min_item = self.Orders.min_item() min_i = min_item[1] # 1.enough # 2.not enough if min_i.amount <= current_amount: current_amount -= min_i.amount self.totalAmount -= min_i.amount to_pop_orders.append(self.Orders.pop_min()[1]) continue # this is a hard case , need to take care of # first we split the order , take amount from current order # and make elif min_i.amount > current_amount: new_order = min_i.copy() new_order.amount = current_amount to_pop_orders.append(new_order) min_i.amount -= current_amount self.totalAmount -= current_amount current_amount = 0 break # return: # 1.orders # 2.amount to trade # 3.amount left return to_pop_orders def remove(self, order): k = order.timestamp self.totalAmount -= self.Orders[k].amount self.Orders.remove(k) def get_price_depth(self): return self.totalAmount def is_empty(self): return self.Orders.count == 0