import numpy as np
import time
# 创建一个包含10000个数的列表和数组
= list(range(100000))
python_list = np.array(range(100000))
numpy_array
# 测试Python列表计算时间
= time.time()
start_time = [x * 2 for x in python_list]
python_result = time.time() - start_time
python_time
# 测试NumPy数组计算时间
= time.time()
start_time = numpy_array * 2
numpy_result = time.time() - start_time
numpy_time
print(f"Python列表计算时间: {python_time:.6f}秒")
print(f"NumPy数组计算时间: {numpy_time:.6f}秒")
Python列表计算时间: 0.002609秒
NumPy数组计算时间: 0.000521秒