Speed

Janos Blazi jblazi at netsurf.de
Sun May 14 07:39:39 EDT 2000


Please look at the question at the end of this long posting.

Here is a Python program:
--------------------------------------------------------
global r
r=[]

def neue_zeile(z):
    global r
    z.reverse();
    r=[]
    last=-1
    for x in z:
        if x==last: cnt=cnt+1
        else:
            if last != -1:
                r = [cnt,last] + r
            last=x
            cnt=1
    r = [cnt,last] + r

l=[1]
for i in range(0,41):
    print i,len(l)
    neue_zeile(l)
    l=r
print i,len(l)

print "*done*"
----------------------------------------------------

And here the same program (as far I can tell) in C:

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

#include <LEDA/basic.h>
#include <LEDA/list.h>

list<int> R,L;

void neue_zeile(list<int>& z)
{
  int last,cnt,x;

  z.reverse_items();
  R.clear();
  last=-1;
  forall(x,L) {
    if (x==last) ++cnt;
    else {
      if (last != -1) {R.push(last); R.push(cnt);}
      last=x;
      cnt=1;
      }
    }
  R.push(last); R.push(cnt);
}

int main()
{
  int i;
  L.push(1);

  for(i=0;i<41;i++) {
     cout << i << " : " << L.length() << "\n";
     neue_zeile(L);
     L=R;
     }
  cout << i << " : " << L.length() << "\n";

  return 0;
}

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

Now it is clear that C is faster than Python, so the C program will have a
shorter execution time.
But I cannot believe that C can be hundreds of times faster (The C code need
less than 2 seconds on my NT machine and the Python code more than 17
minutes.)

Is my Python code stupid? Can I improve it?

Janos Blazi




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list