Ask Your Question
1

What are all the types we can declare in Cython?

asked 11 years ago

Let me be more precise: I know we can declare "int", "list", "float", "double", but what else ?

Is it possible to declare a matrix ?
Is it possible to declare combined types, as list of list of integers...?

My goal is that the program doesn't lose time to find, for example, in which type are the elements of a list...

If it's to long to describe all such available types here, maybe you can indicate to me where I can find this information.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 11 years ago

Volker Braun gravatar image

The biggest speed benefit comes from using C primitive types instead of Python objects, for example machine integers instead of Python / Sage integer objects. Of course thats a pretty small list of types.

You can also declare any Cython cdef class, but that mostly saves you from having to cast the Python object to the cdef class all the time. Not so much speed benefit.

Python lists are still hash tables containing Python objects even if you declare them as cdef list foo. In particuar, it is not possible to have a memory-contiguous array of a particular C datatype that way. You shoud use C arrays or C++ std::vector if you need arrays of a fixed C datatype.

Note that your question smells of premature optimization. If you haven't benchmarked your Python implementation yet then don't bother.

Preview: (hide)
link
2

answered 11 years ago

niles gravatar image

The documentation at http://www.cython.org/ is probably a good place to start. After that, you could look at the Sage source code, either in your local install or at http://hg.sagemath.org/sage-main/src. For example, you can see that other types are declared in sage/matrix/misc.pyx.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 6,415 times

Last updated: Jul 03 '13