I want to update a list of objects in python as fast as possible. What I am doing right now will be demonstrated in the following code:
from bokeh.models.sources import ColumnDataSource
from random import randint
n = 10
m = 50
sources = []
for i in range(n):
# all list elements have similar structure
sources.append(ColumnDataSource(data=dict(x=range(m), y=range(m), count=[0])))
def some_function():
# do some computation
return [randint(0, m) for i in xrange(n)]
def update():
# this function is called every 20ms
for s in sources:
s.data = dict(x=some_function(), y=s.data['y'], count=[s.data['count'][0]+1])
The for loop of my update() function takes too long. I have a lot of lists to update and the function is called every 20 ms. Sometimes the update() function takes more than 20ms to execute.
From my current research I know that list comprehensions are much faster than for loops but I cannot use them in my case, can I? Like:
#not working code
sources = [dict(x=.., y=.., count=..) for s.data in sources]