Python and STL efficiency
Maric Michaud
maric at aristote.info
Wed Aug 23 05:27:29 EDT 2006
Tim, sorry for I send it to you personally, I was abused by thunderbird.
Tim N. van der Leeuw a écrit :
>
> Your C++ version got me the following timings (using gcc 3.4.5 as the
> compiler, MinGW version, with -O6):
>
> ...
>
>
> Hmmm... Can we conclude now that carefully crafted C++ code is about
> twice as fast as casually and intuitively written Python code? ;) (Just
> kidding here of course)
Every C++ code must be carefully crafted :).
But your benchmark surprise me, here is what I get on my windows box :
Python2.4 :
===========
maric at aristote-2 /cygdrive/c/Documents and Settings/maric/Bureau
$ which python2.4
/usr/bin/python2.4
maric at aristote-2 /cygdrive/c/Documents and Settings/maric/Bureau
$ which python
/cygdrive/c/Python24/python
maric at aristote-2 /cygdrive/c/Documents and Settings/maric/Bureau
$ python2.4 testpython.py
so long...
What do you know
fool
chicken crosses road
Elapsed: 3.655000 seconds
maric at aristote-2 /cygdrive/c/Documents and Settings/maric/Bureau
$ python testpython.py
so long...
What do you know
fool
chicken crosses road
Elapsed: 3.077764 seconds
Cygwin version is slower, but not too much.
C++, compiled with VS2005, release, no other configuration :
============================================================
print_occurence_of_strings
What do you know?
chicken crosses road
fool
so long...
print_occurence_of_unique_strings
What do you know?
chicken crosses road
fool
so long...
print_occurence_of_unique_strings_compared_by_address
What do you know?
chicken crosses road
fool
so long...
strings : 16.718 # memory allocation is quite bad
unique strings : 1.188
compared by address : 0.453
C++, with gcc 3.3/Cygwin
========================
maric at aristote-2 /cygdrive/c/Documents and Settings/maric/Bureau
$ g++ -O3 -o testcpp testcpp.cpp
maric at aristote-2 /cygdrive/c/Documents and Settings/maric/Bureau
$ ./testcpp
print_occurence_of_strings
What do you know?
chicken crosses road
fool
so long...
print_occurence_of_unique_strings
What do you know?
chicken crosses road
fool
so long...
print_occurence_of_unique_strings_compared_by_address
What do you know?
chicken crosses road
fool
so long...
strings : 17.266 # still bad
unique strings : 1.547
compared by address : 0.375
Hum, with my old gcc I get equal or better performances than with VS2005.
Finally, the equivalent code is still about 10x faster in c++ than in
Python, as it was on my Linux box.
Mc Osten a écrit :
...
> However, I would have written the code using a proper compare function
> rather than using two sets. In this particular case the number of
> elements of the first set is negligible in respect of the initial vector
> size, thus copying it again does not take a lot of time.
> But such code is optimized for the problem itself: in the real world I
Of course, it's a quick hack to get the same behavior.
> suppose we would have passed set a proper comparison function that
> checks address and then string equality.
>
Yes, furthermore, that is *exactly* what the python code is doing (see
my other post to Fredrik).
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
More information about the Python-list
mailing list