Efficiency comparison between python lists and arrays

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.


insert image description here


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,
insert image description here
which clearly shows the efficiency difference between lists and arrays.


Guess you like

Origin blog.csdn.net/weixin_48964486/article/details/123653320