Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List comprehension:
#1
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

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.0937495231628418
Thanks for any feedback or advice

Paul
Reply
#2
https://stackoverflow.com/questions/2210...-for-loops
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list comprehension invalid syntax mcgrim 1 4,599 Jun-12-2019, 08:28 PM
Last Post: Yoriz
  Python List Comprehension. rinu 3 3,013 Jan-08-2019, 12:30 PM
Last Post: perfringo
  Help me understand a simple list comprehension PiPy 6 3,890 Oct-17-2018, 07:59 PM
Last Post: PiPy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020