How to make python run faster

Ò

Ò»Ê×Ê«

I read this article on http://kortis.to/radix/python_ext/

And I decided to try if it's true.

I write the program in 4 ways:

1. Pure C
2. Python using C extension
3. Python using psycho
4. Pure Python

And then I used timeit to test the speed of these 4. Unsurprisingly,
the time they cost were:

4 > 3 > 2 > 1

But I did noticed that 2 is a least 3 times slower than 1, not as fast
as the article stated.

That's quite weird and I thought maybe it's because I am using
Windows. I did the same test on Linux and I found 2 only uses 1.5
times of time of 1.

But, it is still not as fast as 1.
 
L

lbonafide

But, it is still not as fast as 1.


So if speed is the #1 design goal, use pure C. If not, develop in
pure Python and, if the application is too slow, profile the code and
look for bottlenecks that can be optimized. There's a good chance
that they can be resolved algorithmically, not by simply dropping down
to C.
 
K

king kikapu

I read this article onhttp://kortis.to/radix/python_ext/

And I decided to try if it's true.

I write the program in 4 ways:

1. Pure C
2. Python using C extension
3. Python using psycho
4. Pure Python

And then I used timeit to test the speed of these 4. Unsurprisingly,
the time they cost were:

4 > 3 > 2 > 1

But I did noticed that 2 is a least 3 times slower than 1, not as fast
as the article stated.

That's quite weird and I thought maybe it's because I am using
Windows. I did the same test on Linux and I found 2 only uses 1.5
times of time of 1.

But, it is still not as fast as 1.

I have experimented too with this scenario. My conclusion is that it
aint worth it to mess with C. Program only in Python and when the time
comes that you want to speed up something, just use Psyco on this. And
that will be more than enough.
 
G

Gabriel Genellina


Note the date (2002) and the Python version used (2.1)
And I decided to try if it's true.

I write the program in 4 ways:

1. Pure C
2. Python using C extension
3. Python using psycho
4. Pure Python

And then I used timeit to test the speed of these 4. Unsurprisingly,
the time they cost were:

4 > 3 > 2 > 1

But I did noticed that 2 is a least 3 times slower than 1, not as fast
as the article stated.

That's quite weird and I thought maybe it's because I am using
Windows. I did the same test on Linux and I found 2 only uses 1.5
times of time of 1.

But, it is still not as fast as 1.

As other have noted, there are two important things to consider:

1) use the right algorithm and the right data structure for the job.
Usually it's much better to use an O(n) process (if available) than to try
to microoptimize an O(n²) variant.

2) profile and measure your code to find the critical parts, and apply
optimizations ONLY on those.

The algorithm used in the article... hmmm, well, this is a public forum
and there are ladies and minors so I won't use *those* words, but it's
really horrible, or horrible², and that makes the whole article moot.
Python has *other* advantages, apart from speed: easy to write meaningful
code, expresiveness, code easy to understand by others, higher order
constructs, easy to write prototypes, non trivial OO...
Take the good things from Python and delegate the speed, when required, to
Cython or psyco or a C extension or a normal C library+ctypes.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top