Cython Speed Boost vs. Usability [closed]

2024/11/18 21:34:20

I just came across Cython, while I was looking out for ways to optimize Python code. I read various posts on Stack Overflow, the python wiki and read the article "General Rules for Optimization".

Cython is something which grasps my interest the most; instead of writing C-code for yourself, you can choose to have other data types in your python code itself.

Here is a silly test I tried,

#!/usr/bin/python
# test.pyx
def test(value):for i in xrange(value):i**2if(i==1000000):print itest(10000001)

$ time python test.pyx

real    0m16.774s 
user    0m16.745s
sys     0m0.024s

$ time cython test.pyx

real    0m0.513s 
user    0m0.196s 
sys     0m0.052s

Now, honestly, I'm dumbfounded. The code which I have used here is pure python code, and all I have changed is the interpreter. In this case, if the cython is this good, then why do people still use the traditional Python interpreter? Are there any reliability issues for Cython?

Answer

The other answers have already explained how you were just compiling the Cython code, not executing it. However, I thought that you might want to know how much faster Cython can make your code. When I compiled the code you have (though I ran the function from from a different module) with distutils, I got very marginal speed gains over straight Python – about 1%. However, when I added a few small changes to your code:

def test(long long value):cdef long long icdef long long zfor i in xrange(value):z = i**2if(i==1000000):print iif z < i:print "yes"

and compiled it, I got the following times:

  • Pure Python code: 20.4553578737 seconds
  • Cython code: 0.199339860234 seconds

That’s a 100× speed-up. Not too shabby.

https://en.xdnf.cn/q/26514.html

Related Q&A

cc1plus: warning: command line option -Wstrict-prototypes is valid for Ada/C/ObjC but not for C++

I am building a C++ extension for use in Python. I am seeing this warning being generated during the compilation process - when a type:python setup.py build_ext -iWhat is causing it, and how do I fix i…

Any way to add a new line from a string with the \n character in flask?

I was playing around with flask when I came across an odd problem with the \n character. it dosent seem to have an effect in my browser, I tried putting in there but it didnt work, any ideas?from fla…

Get list of Cache Keys in Django

Im trying to understand how Django is setting keys for my views. Im wondering if theres a way to just get all the saved keys from Memcached. something like a cache.all() or something. Ive been trying t…

numpy: multiply arrays rowwise

I have those arrays:a = np.array([[1,2],[3,4],[5,6],[7,8]])b = np.array([1,2,3,4])and I want them to multiply like so:[[1*1, 2*1], [3*2, 4*2], [5*3, 6*3], [7*4, 8*4]]... basically out[i] = a[i] * b[i],…

django post_save signals on update

I am trying to set up some post_save receivers similar to the following: @receiver(post_save, sender=Game, dispatch_uid=game_updated) def game_updated(sender, **kwargs):DO SOME STUFF HEREMyPick.objects…

Fastest way to pack a list of floats into bytes in python

I have a list of say 100k floats and I want to convert it into a bytes buffer.buf = bytes() for val in floatList:buf += struct.pack(f, val) return bufThis is quite slow. How can I make it faster using …

PyPI is slow. How do I run my own server?

When a new developer joins the team, or Jenkins runs a complete build, I need to create a fresh virtualenv. I often find that setting up a virtualenv with Pip and a large number (more than 10) of requi…

How to suppress the deprecation warnings in Django?

Every time Im using the django-admin command — even on TAB–completion — it throws a RemovedInDjango19Warning (and a lot more if I use the test command). How can I suppress those warnings?Im using D…

whats the fastest way to find eigenvalues/vectors in python?

Currently im using numpy which does the job. But, as im dealing with matrices with several thousands of rows/columns and later this figure will go up to tens of thousands, i was wondering if there was …

Python Patch/Mock class method but still call original method

I want to use patch to record all function calls made to a function in a class for a unittest, but need the original function to still run as expected. I created a dummy code example below:from mock im…