pypy and ctypes

P

Peter Chant

I'm looking to speed up some python code. Replacing the python
interpreter with pypy was impressive. I noted that use of ctypes (in
cython?), specifically declaring variables as below, was reported as
giving a useful result:

cdef float myvar
cdef int i

under cython can provide a useful speed increase even if sections of
code are not re-written in C. Does this also work in PyPy? From
googling I keep getting pointed at ffi - but that seems to relate more
to calling c-libraries rather than what I'm trying to do - seeing if
there are any simple speed ups by declaring variables.

Or is it that - if I keep the code as simple as possible, PyPy is about
as fast as you can get?

Pete
 
N

Neil Cerutti

Or is it that - if I keep the code as simple as possible, PyPy
is about as fast as you can get?

PyPy profiles your code as it runs and creates, using a
just-in-time compiler, highly optimized versions of frequently
run sections. You don't have to declare types or even think about
it; The scheme will work best with code that runs for a
significant amount of time.

cython allows you to declare types, and attempts to create more
efficient code *at compile time* using those type declaration.

Which approach will be better depends on how the code runs and
how clever you are at using cython.

PyPy isn't designed to speed up programs that run for a few
hundred milliseconds and then complete, though it might sometimes
work for that.
 
P

Peter Chant

PyPy profiles your code as it runs and creates, using a
just-in-time compiler, highly optimized versions of frequently
run sections. You don't have to declare types or even think about
it; The scheme will work best with code that runs for a
significant amount of time.

That is the situation I'm in. If it only ran for a second or two
there'd be no point in worrying about speed. PyPy gave a massive speed
up. I was wonding if I could do a little more.
cython allows you to declare types, and attempts to create more
efficient code *at compile time* using those type declaration.

Which approach will be better depends on how the code runs and
how clever you are at using cython.

If it is marginal then I don't think the effort would be worth while.
I do wonder whether writing some specific functions in C using cffi
would benefit. It is something I have no experience of.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top