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<stringa;
for (long int i=0; i<10000 ; ++i){
a.push_back("Wh at do you know?");
a.push_back("so long...");
a.push_back("ch icken crosses road");
a.push_back("fo ol");
}
set<stringb(a.b egin(), a.end());
unique_copy(b.b egin(), b.end(), ostream_iterato r<string>(cout , "\n"));
}
#python
def f():
a = []
for i in range(10000):
a.append('What do you know')
a.append('so long...')
a.append('chick en 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?
efficiency of python and STL.
//C++
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main(){
vector<stringa;
for (long int i=0; i<10000 ; ++i){
a.push_back("Wh at do you know?");
a.push_back("so long...");
a.push_back("ch icken crosses road");
a.push_back("fo ol");
}
set<stringb(a.b egin(), a.end());
unique_copy(b.b egin(), b.end(), ostream_iterato r<string>(cout , "\n"));
}
#python
def f():
a = []
for i in range(10000):
a.append('What do you know')
a.append('so long...')
a.append('chick en 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?
Comment