Oct-29-2021, 04:56 PM
Hi
I'm trying to figure out how a list comprehesion works from a "memory" point of view?
I mean that append corresponds to a dynamic allocation memory, but what about the list comprehension?
In other word, I'm wondering if I can speed up my code using both matrix initialization and list comprehension when I cannot use vectorization.
Here is my small test case
Paul
I'm trying to figure out how a list comprehesion works from a "memory" point of view?
I mean that append corresponds to a dynamic allocation memory, but what about the list comprehension?
In other word, I'm wondering if I can speed up my code using both matrix initialization and list comprehension when I cannot use vectorization.
Here is my small test case
import numpy as np
import time
n=10_000_000
# using append
t0=time.time()
Matrix0=[]
for i in range(n):
Matrix0.append(i)
t1=time.time()
print(f"Duration with append: {t1-t0}")
# with loops
t0=time.time()
Matrix1=np.zeros(n, dtype=int)
for i in range(n):
Matrix1[i]=i
t1=time.time()
print(f"Duration with loops: {t1-t0}")
# with list comprehension
t0=time.time()
Matrix2=[i for i in range(n)]
t1=time.time()
print(f"Duration with list comprehension: {t1-t0}")
#with vectorization
t0=time.time()
Matrix3=np.zeros(n, dtype=int)
i = np.arange(n)
Matrix3[i]=i
t1=time.time()
print(f"Duration with vectorization: {t1-t0}")Output:Duration with append: 2.374998092651367
Duration with loops: 2.8437514305114746
Duration with list comprehension: 0.7031333446502686
Duration with vectorization: 0.0937495231628418Thanks for any feedback or advicePaul
