[Numpy-discussion] recarray field names

Fernando Perez Fernando.Perez at colorado.edu
Wed Mar 15 16:37:22 EST 2006


Fernando Perez wrote:

> In addition
> 
> if name in t.dtype.fields
> 
> is faster than:
> 
> if name in t.dtype.fields.keys()
> 
> While both are O(N) operations, the first requires a single call to the hash 
> function on 'name' and then a C lookup in the dict's internal key table as a 
> hash table, while the second is a direct walkthrough of a list with 
> python-level equality testing.

[ sorry, copy-pasted wrong timing run]

In [1]: nkeys = 5000000

In [2]: keys=range(nkeys)

In [3]: dct = dict(zip(keys,[None]*len(keys)))

In [4]: time bool(-1 in dct)
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00
Out[4]: False

In [5]: time bool(-1 in keys)
CPU times: user 0.32 s, sys: 0.00 s, total: 0.32 s
Wall time: 0.33
Out[5]: False


Cheers,

f




More information about the NumPy-Discussion mailing list