Real-world Python code 700 times slower than C
Henrik Motakef
henrik at moskau.hmotakef.homeip.net
Sat Jan 5 08:35:30 EST 2002
brent.burley at disney.com (Brent Burley) writes:
> What I really want is something spiritually equivalent to a portable
> inline assembly language with python-ish syntax that generates really
> fast native code and seamlessly integrates with python. I can dream
> can't I?
Sorry, I don't know something like this. However, mind that there are
other languages than Python and C.
For example, I tried how your function performs in OCaml. The code
looks like this:
------
let ramp result size start stop =
let step = (stop - start) / (size - 1) in
for i = 0 to size - 1 do
result.(i) <- start + step * i
done
let main () =
let array = Array.make 10000 0 in
for i = 0 to 100000 do
ramp array 10000 0 1
done
let _ = main ()
------
The only difference is that start and stop (end is a reserved word in
OCaml) are integers. OCaml quite strictly separates Integer and Float
arithmetic, and I was to lazy to care... However, I think you get the
idea anyways.
The results of running the differerent versions of ramp on my 600MHz
Duron with 196MB RAM and OpenBSD:
------
#time ./ramp.py
real 0m4.653s
user 0m4.639s
sys 0m0.000s
#time ./ramp_ml_bytecode
real 0m0.243s
user 0m0.234s
sys 0m0.001s
#time ./ramp_c
real 0m9.412s
user 0m9.390s
sys 0m0.008s
#time ./ramp_ml_native
real 0m12.283s
user 0m12.257s
sys 0m0.008s
#time ./ramp_ml_unsafe
real 0m10.066s
user 0m10.029s
sys 0m0.008s
-----
ramp.py and ramp_c are taken unchanged from your posting, ramp_c is
compiled with "gcc -o ramp_c -O2 ramp.c". ramp_ml_bytecode is
compiled to platform independent bytecode, and, like ramp.py, iterates
only 100 times. ramp_ml_native and ramp_ml_unsafe do, like ramp_c,
100.000 iterations, and are compiled to native, standalone
machine-code. ramp_ml_unsafe used the -unsafe compiler option, turning
off bounds-checking, so that you can have C-style segfaults even
without C :)
I think OCaml may be an option if execution speed is important, but
you still want to have readable syntax (O.K. - once you get used to
it), garbage collection, and generally most of the things C lacks.
However, I don't know a simple way of interfacing OCaml and Python,
you'll probably stil need some C for glueing them together.
IIRC there once were a project (called Viper?) that tried to implement
a Python interpreter in OCaml, but I don't know how far it got. Does
anybody know more?
Regards
Henrik
More information about the Python-list
mailing list