For lists and arrays of 100,000 data, square each number separately. Compare the operating efficiency of the program according to the time consumed by the program execution.
Article directory
1. Guide package
Pour into the time and numpy libraries in turn.
import time
import numpy as np
2. List
Complete in a list and timed.
# 列表
t1 = time.time()
a = []
for x in range(100000):
a.append(x**2)
t2 = time.time()
t = t2 - t1
print(t)
3. Array
Done as an array, and time it.
# 数组
t1 = time.time()
b = np.arange(100000)**2
t2 = time.time()
t = t2 - t1
print(t)
__
4. Execution results
The program execution results are shown in the figure,
which clearly shows the efficiency difference between lists and arrays.