简体   繁体   中英

How can I check time taken while creating a million ndarrays?

I'm trying to figure out how long will it take to create a million of ndarray (numpy array) and compare it to the time taken by list with the timeit library but I'm getting the following syntax error:

File "<timeit-src>", line 2
    [1 2 3 4 5]
       ^
SyntaxError: invalid syntax

And this error makes sense but I don't even know how can I do this.

The following is code I tried so far

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=': ')

on list result is as an expected one but on ndarray is not.

You can pass a callable:

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...?)

You can also pass a string, but as a comment has alluded to here, you'll need to set it up properly:

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]                                                                            
In [629]: ndarr=np.array([1,2,3,4,5])                                                                
In [630]: f'{arr}'                                                                                   
Out[630]: '[1, 2, 3, 4, 5]'
In [631]: f'{ndarr}'                                                                                 
Out[631]: '[1 2 3 4 5]'

The problem is that while "[1, 2, 3, 4, 5]" is an executable expression, "[1 2 3 4 5]" is not. It's hard to create an executable expression from an existing ndarray - not impossible, but you have to work at it.

In an ipython session, it's simple to run such timeit tests:

In [632]: timeit [1,2,4,5]                                                                           
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])                                                                 
2.22 µs ± 20.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

The repr is better, looking more list-like

In [635]: f'{repr(ndarr)}'                                                                           
Out[635]: 'array([1, 2, 3, 4, 5])'

but even that needs tweaking to get the array part right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM