numpy min max is twice as fast as the native method
import time as t
import numpy as np
def initialize():
storage.reset()
def tick():
array = data.btc_usd.period(250, 'close')
t1 = t.time()
a = min(array)
b = max(array)
t2 = t.time()
c = np.min(array)
d = np.max(array)
t3 = t.time()
storage.t1 = storage.get('t1', 0)
storage.t2 = storage.get('t2', 0)
storage.t1 += t2-t1
storage.t2 += t3-t2
def stop():
log('python: %.5f' % storage.t1)
log('numpy: %.5f' % storage.t2)
log('ticks: %s' % info.tick)
yeilds:
[2015-11-06 10:00:00] python: 0.45959
[2015-11-06 10:00:00] numpy: 0.26148
[2015-11-06 10:00:00] ticks: 7426
but I think you're looking for something more like this:
import time as t
import numpy as np
def initialize():
storage.reset()
def tick():
storage.closes = storage.get('closes', [])
if info.tick == 0:
storage.closes = [float(x) for x in data.btc_usd.period(250, 'close')]
else:
z = storage.closes.pop(0) #pop left
price = float(data.btc_usd.close)
storage.closes.append(price) #append right
array = np.array(storage.closes)[-250:]
# now we know 'z' just left the list and 'price' just entered
# otherwise the array is the same as the previous example
t1 = t.time()
# PYTHON METHOD
a = min(array)
b = max(array)
t2 = t.time()
# NUMPY METHOD
c = np.min(array)
d = np.max(array)
t3 = t.time()
# STORAGE METHOD
storage.e = storage.get('e', 0)
storage.f = storage.get('f', 0)
if info.tick == 0:
storage.e = np.min(array)
storage.f = np.max(array)
else:
if z == storage.e:
storage.e = np.min(array)
if z == storage.f:
storage.f = np.max(array)
if price < storage.e:
storage.e = price
if price > storage.f:
storage.f = price
t4 = t.time()
storage.t1 = storage.get('t1', 0)
storage.t2 = storage.get('t2', 0)
storage.t3 = storage.get('t3', 0)
storage.t1 += t2-t1
storage.t2 += t3-t2
storage.t3 += t4-t3
def stop():
log('python: %.5f' % storage.t1)
log('numpy: %.5f' % storage.t2)
log('storage: %.5f' % storage.t3)
log('ticks: %s' % info.tick)
yeilds:
[2015-11-06 10:00:00] python: 0.45694
[2015-11-06 10:00:00] numpy: 0.23580
[2015-11-06 10:00:00] storage: 0.16870
[2015-11-06 10:00:00] ticks: 7426
which brings us down to about 1/3rd of the native method with a 7500 iterations against a list of 250
heapq. - falsetruheapqgives you a min-heap or a max-heap, but not a min-max heap. - Martijn Pietersheapqobjects. one with orignal values, one with negated values. - falsetru