可能重複:
construct orderbook from orders example最快獲得500 000收集的一些成員
我想改一下我剛纔的問題construct orderbook from orders example,因爲它非常糟糕的措辭請不要閱讀:)
我收到訂單更新,每個新訂單有+1 orderId:
new orderid = 1 price = 100 quantity = 10
new orderid = 2 price = 101 quantity = 10
modify orderid = 1 price = 100 quantity = 8
new orderid = 3 price = 100 quantity = 7
new orderid = 4 price = 101 quantity = 3
delete orderid = 1
and so on
在每個順序更新我需要計算簿,delete orderid = 1
後,這將是:
price = 100 totalQuantity = 7
price = 101 totalQuantity = 13
讓我們假設最初兩個orders
和orderbook
是空
我想我應該寫一些東西像這樣:
if (order.action = add)
orderBook[order.price] += order.quantity
if (order.action = delete)
orderBook[order.price] -= getLastQuantity(order.id)
if (order.action = modify)
orderBook[order.price] += order.quantity - getLastQuantity(order.id)
大概你能提出不同的實現,但這個是最微不足道的,我認爲 的問題是:如何實現getLastQuantity(int orderId)
簡單的實現:
int[] lastQuantity = new int[100000000];
public int getLastQuantity(int orderId) {
return lastQuantity[orderId];
}
佔用太多的內存,我甚至不知道有多快是
另一種簡單的實現是使用Dictionary
,但我有去逐個我如何可以利用這一點來提高性能比較下令INT鍵?
請注意,一旦訂單被刪除,我不需要記住它的「lastQuantity」。我只需要活動訂單的「最後數量」。
- 訂單總數不超過100 000 000
- 活動訂單是少於500 000
使用數據庫? – 2012-03-29 19:15:15
@DBM它會比使用'Dictionary'存儲500 000個活動訂單的訂單要慢 – javapowered 2012-03-29 19:16:51
請勿重新發布,編輯和改進您的問題。人們已經花時間和麻煩來回答你了。 – 2012-03-29 19:24:41