I could use some help making this Python code run faster using only Python code.

Python Maniac raychorn at hotmail.com
Sat Sep 22 01:25:20 CEST 2007


On Sep 21, 3:02 pm, "Matt McCredie" <mccre... at gmail.com> wrote:
> > Now I think I will code this little scrambler using nothing but the D
> > Language just to see whether there is any benefit in using D over
> > Python for this sort of problem.
>
> Isn't D compiled to machine code? I would expect it to win hands down.
> That is, unless it is horribly unoptimized.
>
> Matt

Well D code is compiled into machine code that runs via a VM.

My initial D code ran in about 6 secs as compare with the 2.278 secs
for the optimized Python code.

If I want the D code to run faster than the optimized Python I would
have to use the same Pythonic optimizations as were used in Python
when crafting the D code and then I would guess the optimized D code
might run only 2x faster than the optimized Python code.

In real terms < 3 secs to process a 20 MB file is more than reasonable
performance with no need to perform any additional optimizations.

For this particular problem Python performs as well as the D powered
machine code using far less effort, for me, than what it would take to
make the D code run faster than the Python code.

All this tells me the following:

* List Comprehensions are very powerful for Python.
* String translation is a must when processing string based data in an
iterative manner.
* Ruby has no hope of being able to beat Python for this type of
problem given the proper Python optimizations are used.
* There is no value in wasting time with lower-level languages to make
Python run faster for this type of problem.

It would be nice if Python could be made to automatically detect the
LC and string translation patterns used by the unoptimized Python code
and make them into optimized Python code on the fly at runtime.  I am
more than a little amazed nobody has chosen to build a JIT (Just In-
Time compiler) or cached-compiler into Python but maybe that sort of
thing is just not needed given the fact that Python code can be easily
optimized to run 30x faster.




More information about the Python-list mailing list