Python and STL efficiency
andrei.zavidei at gmail.com
andrei.zavidei at gmail.com
Sun Aug 27 06:15:42 EDT 2006
---------------------------- C++ --------------------------
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
#include <windows.h>
using namespace std;
int main()
{
DWORD ticks = ::GetTickCount();
const string s1("What do you know");
const string s2("So long...");
const string s3("chicken crosses road");
const string s4("fool");
typedef vector<const string*> str_vector_t;
typedef str_vector_t::iterator vec_iter;
typedef set<const string*> str_set_t;
typedef str_set_t::const_iterator set_iter;
const int size = 1000000;
str_vector_t vec;
vec.reserve(size*4);
for(int i = 0; i < size; ++i){
vec.push_back(&s1);
vec.push_back(&s2);
vec.push_back(&s3);
vec.push_back(&s4);
}
vec_iter new_end = unique(vec.begin(), vec.end());
str_set_t set_(vec.begin(), new_end);
for(set_iter it = set_.begin(); it != set_.end(); ++it){
cout<<*it<<endl;
}
cout<<::GetTickCount()-ticks<<endl;
return 0;
}
In MS VC+ 2005 in release configuration it gets the work done in 187
ms.
---------------- Python + Psyco----------------
def f():
a = []
for i in range(1000000):
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
import time
from time import clock
import psyco
psyco.full()
f_start = clock()
f()
f_end = clock()
print "Elapsed: %f seconds" % (f_end - f_start)
In Python in manages to do the same in 1.8 secs (psyco boosts it to
0.7; see below)
so long...
What do you know
fool
chicken crosses road
Elapsed: 0.772899 seconds
Well, that's how it is in my book.
Regards,
Andrei
More information about the Python-list
mailing list