Last active
August 29, 2015 13:56
-
-
Save lqdc/9149772 to your computer and use it in GitHub Desktop.
Memory Access Microbenchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def initTrades(trades): | |
for i, trade in enumerate(trades): | |
if i % 2: | |
trade["side"] = 'S' | |
else: | |
trade["side"] = 'B' | |
trade["tradeId"] = i | |
trade["clientId"] = 1 | |
trade["venueId"] = 123 | |
trade["instrumentCode"] = 321 | |
trade["price"] = i | |
trade["quantity"] = i | |
def perfRun(runNum, trades): | |
start = time.time() * 1000 | |
initTrades(trades) | |
buyCost, sellCost = 0, 0 | |
for trade in trades: | |
if trade["side"] == 'B': | |
buyCost += trade["price"] * trade["quantity"] | |
else: | |
sellCost += trade["price"] * trade["quantity"] | |
print runNum, "- duration" , ((time.time() * 1000) - start), "ms" | |
print "buyCost = ", buyCost, " sellCost = ", sellCost | |
if __name__ == '__main__': | |
NUM_RECORDS = 500 * 1000# * 444 | |
PyMemTrade = {"tradeId": 0, "clientId": 0, "venueId": 0, | |
"instrumentCode": 0, "price": 0, "quantity": 0, "side": "0"} | |
trades = [PyMemTrade.copy() for i in xrange(NUM_RECORDS)] | |
for i in range (0, 5): | |
perfRun(i, trades) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
NUM_RECORDS = 50 * 1000 * 50 | |
class PyMemTrade(): | |
def __init__(self, *args, **kwargs): | |
self.__dict__.update(kwargs) | |
def initTrades(trades): | |
for i, trade in enumerate(trades): | |
trade.tradeId = i | |
trade.clientId = 1 | |
trade.venueId = 123 | |
trade.instrumentCode = 321 | |
trade.price = i | |
trade.quantity = i | |
if (i % 2 == 0): | |
trade.side = 'B' | |
else: | |
trade.side = 'S' | |
def perfRun(runNum, trades): | |
start = time.time() * 1000 | |
initTrades(trades) | |
buyCost, sellCost = 0, 0 | |
for trade in trades: | |
if trade.side == 'B': | |
buyCost += trade.price * trade.quantity | |
else: | |
sellCost += trade.price * trade.quantity | |
print runNum, "- duration" , ((time.time() * 1000) - start), "ms" | |
print "buyCost = ", buyCost, " sellCost = ", sellCost | |
if __name__ == '__main__': | |
trades = [PyMemTrade(0,0,0,0,0,0,'0') for i in xrange(NUM_RECORDS)] | |
for i in xrange(5): | |
perfRun(i, trades) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python faster_py.py | |
0 - duration 196.531982422 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
1 - duration 191.945068359 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
2 - duration 192.145996094 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
3 - duration 192.202148438 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
4 - duration 192.083007812 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
python pandas_python.py | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
0 - duration 71.9406738281 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
1 - duration 69.2270507812 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
2 - duration 68.9450683594 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
3 - duration 68.9328613281 ms | |
buyCost = 20833208333500000 sellCost = 20833333333250000 | |
4 - duration 68.9848632812 ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from pandas import DataFrame | |
import numpy as np | |
def initTrades(trades): | |
trades['side'][trades.index % 2 == 0] = 'B' | |
trades['side'][trades.index % 2 == 1] = 'S' | |
for k in ['tradeId', 'price', 'quantity']: | |
trades[k] = np.array(trades.index) | |
trades["venueId"] = 123 | |
trades["instrumentCode"] = 321 | |
def perfRun(runNum, trades): | |
start = time.time() * 1000 | |
initTrades(trades) | |
result = trades['price'] * trades['quantity'] | |
buyCost = result[trades["side"] == 'B'].sum() | |
sellCost = result[trades["side"] == 'S'].sum() | |
print "buyCost =", buyCost, "sellCost =", sellCost | |
print runNum, "- duration" , ((time.time() * 1000) - start), "ms" | |
if __name__ == '__main__': | |
NUM_RECORDS = 500 * 1000 #* 444 | |
PyMemTrades = {} | |
for k in ["tradeId", "clientId", "venueId", "instrumentCode", "price", | |
"quantity"]: | |
PyMemTrades[k] = np.zeros(NUM_RECORDS) | |
PyMemTrades["side"] = np.chararray(NUM_RECORDS) | |
PyMemTrades["side"].fill('0') | |
trades = DataFrame(PyMemTrades) | |
for i in xrange(0, 5): | |
perfRun(i, trades) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment