如何检查创建一百万个 ndarrays 所花费的时间?

我试图弄清楚创建一百万个(numpy 数组)需要多长时间,并将其与库ndarray所花费的时间进行比较,但出现以下语法错误:listtimeit


File "<timeit-src>", line 2

    [1 2 3 4 5]

       ^

SyntaxError: invalid syntax

这个错误是有道理的,但我什至不知道我该怎么做。


以下是我到目前为止尝试过的代码


import numpy as np

import timeit


arr = [1, 2, 3, 4, 5]

ndarr = np.array([1, 2, 3, 4, 5])


list_time = timeit.timeit(stmt=f'{arr}', number=1000000)

ndarray_time = timeit.timeit(stmt=f'{ndarr}', number=1000000)


print('list takes', list_time, sep=': ')

print('ndarray takes', ndarray_time, sep=': ')

结果list与预期一致,但结果ndarray并非如此。


浮云间
浏览 125回答 2
2回答

qq_花开花谢_0

您可以传递一个可调用对象:list_time = timeit.timeit(stmt=lambda: [1, 2, 3, 4, 5], number=1000000)ndarray_time = timeit.timeit(stmt=lambda: np.array([1, 2, 3, 4, 5]), number=1000000)#lambda: (ndarr := np.array([1, 2, 3, 4, 5])) - (if actual assignment matters...?)您也可以传递一个字符串,但正如此处的评论所暗示的那样,您需要正确设置它:list_time = timeit.timeit(stmt="a = [1, 2, 3, 4, 5]", number=1000000)ndarray_time = timeit.timeit(stmt=lambda: "a = np.array([1, 2, 3, 4, 5])", setup="import numpy as np", number=1000000)

慕森王

In [628]: arr=[1,2,3,4,5]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [629]: ndarr=np.array([1,2,3,4,5])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [630]: f'{arr}'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[630]: '[1, 2, 3, 4, 5]'In [631]: f'{ndarr}'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[631]: '[1 2 3 4 5]'问题是 while"[1, 2, 3, 4, 5]"是一个可执行表达式,"[1 2 3 4 5]"但不是。很难从现有的表达式创建可执行表达式ndarray- 并非不可能,但您必须努力。在ipython会话中,运行这样的 timeit 测试很简单:In [632]: timeit [1,2,4,5]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;60.6 ns ± 1.77 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)In [633]: timeit np.array([1,2,4,5])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.22 µs ± 20.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)更好repr,看起来更像列表In [635]: f'{repr(ndarr)}'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[635]: 'array([1, 2, 3, 4, 5])'但即使这样也需要调整才能使array零件正确。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python