how fast is Python?
Alex Martelli
aleax at aleax.it
Thu Aug 21 12:25:51 EDT 2003
Mark Carter wrote:
...
> The following algorithm was implemented in each of the target
> languages:
>
> X = 0.5
> For I = 1 to 108
> X = 1 – X* X
> Next
>
> Timings were made for the execution. The following results were
> obtained:
>
> Language Timing (seconds)
> VB – uncompiled 74
> VB – compiled 37
> VBA – Excel 75
> Python 401
> C++ - debug version 4
> C++ - release version 3
> Fortran 3
Interesting. One wonders what and where you measured, e.g:
[alex at lancelot gmpy]$ cat a.cpp
int main()
{
double X = 0.5;
for(int i = 0; i < 108; i++)
X = 1 + X * X;
return 0;
}
[alex at lancelot gmpy]$ g++ -O3 a.cpp
[alex at lancelot gmpy]$ time ./a.out
0.01user 0.00system 0:00.00elapsed 333%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (186major+21minor)pagefaults 0swaps
i.e., it's just too fast to measure. Not much better w/Python...:
[alex at lancelot gmpy]$ cat a.py
def main():
X = 0.5
for i in xrange(108):
X = 1 + X*X
main()
[alex at lancelot gmpy]$ time python -O a.py
0.03user 0.01system 0:00.15elapsed 26%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (452major+260minor)pagefaults 0swaps
i.e., for all we can tell, the ratio COULD be 100:1 -- or just about
anything else! Perhaps more details are warranted...
Alex
More information about the Python-list
mailing list