Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython - dictionary keys and values static type definition

Tags:

python

cython

I want to use Cython to compile a Python module that works with a dictionary of which all keys are of type, say, integer (or no matter what other static and known type), and all values of type unicode (or other static and known).

Now, to speed it up, I can declare

cdef dict Dict

and also

cdef int k
cdef unicode v

But, can I make a static declaration of the whole "dict int->unicode" structure?

Thanks,

like image 777
Ori5678 Avatar asked Oct 13 '13 08:10

Ori5678


1 Answers

I think the short answer is no. Cython is still using the built-in Python dictionary. It can take advantage of some optimisations if you declare the object as dict, but ultimately the dict has to be able to store objects of different types, and so you cannot specify the key or value types at compile-time. But you should check first to make sure that this is a bottleneck. Python dictionaries are fairly good.

like image 128
Andy Rimmer Avatar answered Nov 01 '22 22:11

Andy Rimmer