Ori5678
Ori5678

Reputation: 499

Cython - dictionary keys and values static type definition

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,

Upvotes: 10

Views: 15855

Answers (1)

Andy Rimmer
Andy Rimmer

Reputation: 2111

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.

Upvotes: 12

Related Questions