Python and STL efficiency

Tim N. van der Leeuw tim.leeuwvander at nl.unisys.com
Tue Aug 22 16:23:50 EDT 2006


Tim N. van der Leeuw wrote:
> Mc Osten wrote:
> > Tim N. van der Leeuw <tim.leeuwvander at nl.unisys.com> wrote:
> >
> > > Oh boy; yes indeed the slow python is faster than the fast C++
> > > version... Must be something really awful happening in the STL
> > > implementation that comes with GCC 3.4!
> >
> > And the Python version does the very same number of iterations than the
> > C++ one? I suppose they are looping on arrays of different sizes, just
> > like my "first version".
> >
>
> Hmmm.. You're quite right. The C++ version had an array size 100.000
> (your version), the Python version still had an array size 10.000 (as
> in my modified copy of the original version).
>
> When fixing the Python version to have 100.000 items, like the C++
> version, the Python timings are:
>
[...]
> Fast - Elapsed: 0.512088 seconds
> Slow - Elapsed: 1.139370 seconds
>
> Still twice as fast as the fastest GCC 3.4.5 compiled version!
>
> Incidentally, I also have a version compiled with VC++ 6 now... (not
> yet w/VC++ 7) .. Compiled with release-flags and maximum optimization
> for speed, here's the result of VC++ 6:
>
> LeeuwT at nlshl-leeuwt ~/My Documents/Python
> $ ./SpeedTest_VC.exe
[...]
> Fast - Elapsed: 4.481 seconds
> Slow - Elapsed: 4.842 seconds
>
[...]

And the results of IronPython (1.0rc2) are just in as well:

IronPython 1.0.60816 on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>>
>>> import sys
>>> sys.path.append('c:/documents and settings/leeuwt/my documents/python')
>>> import SpeedTest
>>> SpeedTest.run_test()
Begin Test
Number of unique string objects: 4
What do you know
so long...
chicken crosses road
fool
Number of unique string objects: 400000
What do you know
so long...
chicken crosses road
fool
Fast - Elapsed: 1.287923 seconds
Slow - Elapsed: 4.516272 seconds
>>>


And for Python 2.5:
LeeuwT at nlshl-leeuwt ~/My Documents/Python
$ /cygdrive/c/Python25/python.exe SpeedTest.py
Begin Test
Number of unique string objects: 4
so long...
What do you know
fool
chicken crosses road
Number of unique string objects: 400000
so long...
What do you know
fool
chicken crosses road
Fast - Elapsed: 0.440619 seconds
Slow - Elapsed: 1.095341 seconds

LeeuwT at nlshl-leeuwt ~/My Documents/Python

But beware! For Python2.5 I had to change the code slightly, because it
already realized that the expression

'%s' % 'something'

will be a constant expression, and evaluates it once only... so I had
to replace '%s' with a variable, and I got the timings above which show
Python2.5 to be slightly faster than Python2.4.

(Next step would be to create a VB version and a Java version of the
same program, oh and perhaps to try a version that would work with
Jython... perhaps somehow w/o the 'set')

Cheers,

--Tim




More information about the Python-list mailing list