Myluzh Blog

Python numpy中ndarray与原生list对比

2023-7-13 myluzh Python

0x01 ndarray与list运行时长对比 import random import numpy as np import timeit a = [] for i in range(100000000): a.append(random.random()) def sum_list(): return sum(a) def sum_numpy(): b = np.array(a) return np.sum(b) time_list = timeit.timeit(sum_list, number=1) time_numpy = timeit.timeit(sum_numpy, number=1) print("List sum time:", time_list) print("Numpy sum time:", time_numpy) List sum time: 6.816154757 Numpy sum time: 4.999972694 0x02 ndarray的优势 (1)ndarray在存储数据的时候,数据...

阅读全文>>

标签: numpy ndarray

评论(0) (320)