Python and STL efficiency

Mc Osten riko at despammed.com
Tue Aug 22 06:55:35 EDT 2006


Jeremy Sanders <jeremy+complangpython at jeremysanders.net> wrote:

> It must be the debugging, the compiler or a poor STL implementation. With
> gcc 4 it runs instantly on my computer (using -O2), even with 10x the
> number of values.

$ gcc --version
i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
5363)

I adapted original poster's code and made a function that did not create
strings each time. The NoisyString is a class we can use to actually
track copying.

In fact Python here is faster. Suppose it has a really optimized set
class...


Here some results (I know that the fpoint optimizations are useless...
it's is my "prebuilt" full optimization macro :) ):




$ g++ -O3 -pipe -O2 -march=pentium-m -msse3 -fomit-frame-pointer
-mfpmath=sse  -o set_impl set_impl.cpp 
$ ./set_impl 
What do you know?
chicken crosses road
fool
so long...
What do you know?
chicken crosses road
fool
so long...
Elapsed 5.8
Elapsed 1.71

$ g++ -Os -pipe -O2 -march=pentium-m -msse3 -fomit-frame-pointer
-mfpmath=sse  -o set_impl set_impl.cpp 
$ ./set_impl 

What do you know?
chicken crosses road
fool
so long...
What do you know?
chicken crosses road
fool
so long...
Elapsed 5.8
Elapsed 1.71

$ g++ -O3 -o set_impl set_impl.cpp 
$ ./set_impl 
What do you know?
chicken crosses road
fool
so long...
What do you know?
chicken crosses road
fool
so long...
Elapsed 0.47
Elapsed 0.18

$ g++ -o set_impl set_impl.cpp 
$ ./set_impl 
What do you know?
chicken crosses road
fool
so long...
What do you know?
chicken crosses road
fool
so long...
Elapsed 0.63
Elapsed 0.33

$ python -O set_impl.py 
so long...
What do you know
fool
chicken crosses road
so long...
What do you know
fool
chicken crosses road
Elapsed: 1.370000 seconds
Elapsed: 3.810000 seconds



------------------- PYTHON CODE ---------------------------------
#python

global size 
size = 1000000

def f():
        a = []
        for i in range(size):
                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

def slow_f():
        a = []
        for i in range(size):
                a.append('%s' % 'What do you know')
                a.append('%s' % 'so long...')
                a.append('%s' % 'chicken crosses road')
                a.append('%s' % 'fool')
        b = set(a)
        for s in b:
                print s

import time
from time import clock

f_start = clock()
f()
f_end   = clock()

slow_f_start = clock()
slow_f()
slow_f_end   = clock()

print "Elapsed: %f seconds" % (f_end - f_start)
print "Elapsed: %f seconds" % (slow_f_end - slow_f_start)

------------------------------------------------------------------


----------------- CPP CODE -------------------------------------
#include <iostream>
#include <ostream>
#include <iterator>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <ctime>
using namespace std;


#define SIZE 1000000

class NoisyString : public std::string {
  public:
  NoisyString(const string& cp) 
      : string(cp) 
  {
      cout << "Fuck I got copied!" << endl;
  }
  
  NoisyString(const char* s ) : string(s) {
    
  }
  
  
  
};


void f(){
  vector<string> a;
        for (long int i=0; i<SIZE ; ++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());
        copy(b.begin(), b.end(), ostream_iterator<string>(cout, "\n"));
}

void fast_f(){
  vector<string> a;
  string s1  =  "What do you know?"   ;
  string s2  =  "so long..."          ;
  string s3  =  "chicken crosses road";
  string s4  =  "fool"                ;
        for (long int i=0; i<SIZE ; ++i){
                a.push_back(s1);
                a.push_back(s2);
                a.push_back(s3);
                a.push_back(s4);
        }
        set<string> b(a.begin(), a.end());
        copy(b.begin(), b.end(), ostream_iterator<string>(cout, "\n"));
}


int main(){
  clock_t f_start, 
          f_end,
          faster_f_start, 
          faster_f_end,
          fast_f_start,
          fast_f_end;
         
  f_start = clock();
  f();
  f_end   = clock();
  
  fast_f_start = clock();
  fast_f();
  fast_f_end   = clock();
  

  cout << "Elapsed " << (f_end - f_start) / double(CLOCKS_PER_SEC) <<
endl;
  cout << "Elapsed " << (fast_f_end - fast_f_start) /
double(CLOCKS_PER_SEC) << endl;
  
}

-----------------------------------------------------------------------




-- 
blog:  http://www.akropolix.net/rik0/blogs | Uccidete i filosofi,
site:  http://www.akropolix.net/rik0/      | tenetevi riso e
forum: http://www.akropolix.net/forum/     | bacchette per voi.



More information about the Python-list mailing list