News for hackers. Stuff that matters.
Re:Pre-Allocated Lists in Python by sharikous on Wednesday March 30, 2022 @04:49AM (#30852796)
You are measuring two things together: allocation and an initialization inside a loop.

I would try to separate them (I.e. measure [None]*length and np.empty(length)

Re:Pre-Allocated Lists in Python by knappa on Wednesday March 30, 2022 @08:59AM (#30854544)

  In [1]: length = 10_000

  In [2]: %%timeit
     ...: pre = [None] * length
     ...: 
     ...: 
  38.7 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
  
  In [3]: %%timeit
     ...: pre = np.empty(length, dtype=int)
     ...: 
     ...: 
  1.32 µs ± 6.59 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)