Marc 'BlackJack' Rintsch wrote:
> In <[EMAIL PROTECTED]>, Licheng Fang
> wrote:
>
> > Hi, I'm learning STL and I wrote some simple code to compare the
> > efficiency of python and STL.
> >
> > //C++
> > #include <iostream>
> > #include <string>
> > #include <vector>
> > #include <set>
> > #include <algorithm>
> > using namespace std;
> >
> > int main(){
> >     vector<string> a;
> >     for (long int i=0; i<10000 ; ++i){
> >             a.push_back("What do you know?");
> >             a.push_back("so long...");
> >             a.push_back("chicken crosses road");
> >             a.push_back("fool");
> >     }
> >     set<string> b(a.begin(), a.end());
> >     unique_copy(b.begin(), b.end(), ostream_iterator<string>(cout, "\n"));
> > }
>
> Why are you using `unique_copy` here?

Sorry, that's a typo. Actually I used 'copy'.
>
> > #python
> > def f():
> >     a = []
> >     for i in range(10000):
> >             a.append('What do you know')
> >             a.append('so long...')
> >             a.append('chicken crosses road')
> >             a.append('fool')
> >     b = set(a)
> >     for s in b:
> >             print s
> >
> > I was using VC++.net and IDLE, respectively. I had expected C++ to be
> > way faster. However, while the python code gave the result almost
> > instantly, the C++ code took several seconds to run! Can somebody
> > explain this to me? Or is there something wrong with my code?
>
> There's a difference in data structures at least.  The Python `set` type
> is implemented with a hash algorithm, so the equivalent STL type would be
> `hash_set`.  `set` in Python does not store its contents sorted.
>
> Ciao,
>       Marc 'BlackJack' Rintsch

Thank you for your comments. I tested with hash_set, but I didn't see
much performance improvement. When I increased the loop to 1 million
times, the python code still ran reasonably fast and the C++ code got
stuck there. This totally surprised me, because according this page
http://norvig.com/python-lisp.html, the speed of python is nowhere near
that of C++.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to