Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,871 questions

51,794 answers

573 users

What is the fastest way to initialize a large array in Python

1 Answer

0 votes
import time
import numpy as np

start_time = time.time()
lst = [1] * 40000000
print("%s seconds" % (time.time() - start_time))

start_time = time.time()
lst = []
lst = [0 for i in range(40000000)] 
print("%s seconds" % (time.time() - start_time))

start_time = time.time()
arr = np.array([1] * 40000000)
print("%s seconds" % (time.time() - start_time))
 
start_time = time.time()
arr = np.array([1 for i in range(40000000)])
print("%s seconds" % (time.time() - start_time))




 
'''
run:
 
0.19373154640197754 seconds
1.5374398231506348 seconds
2.9260823726654053 seconds
3.9488601684570312 second

'''

 



answered Mar 18, 2023 by avibootz

Related questions

1 answer 197 views
1 answer 97 views
1 answer 131 views
1 answer 50 views
1 answer 106 views
...