I have two lists. One is list of datetime assume: d (10000 elements), one is list of int assume: m (1000 elements).
I would like to generate a new list of datetime, which is the shift of m minutes of d.
To put it more intuitively, I am trying to do following code:
[x datetime.timedelta(minutes = y) for x in d for y in m]
The trouble is the speed is very slow for above double loops. Is there any faster way to handle it? Thanks.
CodePudding user response:
To reduce time, just reuse datetime.timedelta(minutes=y)
. For example, trade a little space with the run time.
M = [ datetime.timedelta(minutes=y) for y in range(1000)] # trade space
[x y for x in d for y in M] # save time
Example:
import datetime,time
d = [ datetime.date(2022, 10, _0 1 ) for _ in range(10000)]
m = [ _ for _ in range(1000) ]
M = [ datetime.timedelta(minutes = y ) for y in range(1000)]
start_time = time.time()
[x datetime.timedelta(minutes = y) for x in d for y in m]
print("from time: {}".format(time.time() - start_time))
start_time = time.time()
result = [x y for x in d for y in M]
print("to time: {}".format(time.time()-start_time))
The Output:
from time: 7.302632093429565
to time: 0.6205151081085205