Python vs. Ruby (and os.path.walk)

Steven Atkinson sja at san.rr.com
Fri Aug 9 18:47:03 CEST 2002


Thanks for everyone's response. The test programs I wrote were just
tests to get a quick feeling for how easy it feel to program in each
language.

Much to my embarrassment, I can not reproduce my python slowness right
now. I ran 3 tests yesterday and a few today. All took 3 minutes. I
did some tweaks and sped the code up some, but not a whole lot. Then I
took out all screen IO and really sped the code up. I put the IO back
in and now the code is still acceptable in terms of speed (though
slightly slower than Ruby).

The original code was:
-----------------------------
#!/usr/bin/python
import os
import re

def lister(dummy,dirname, filesindir):
    match = re.compile("(.tl[hb]$)|(_i.c$)|(._p/c$)")
    for fname in filesindir:
        path = os.path.join(dirname,fname)
        if not os.path.isdir(path):
            if match.search(path):
                print path


os.path.walk('c:\\src',lister,None)

I changed it to:
------------------------
#!/usr/bin/python
import os
import re

def lister(match,dirname, filesindir):
    for fname in filesindir: 
	if match.search(fname):
		path = os.path.join(dirname,fname)
		print path


match = re.compile("(.tl[hb]$)|(_i.c$)|(._p/c$)")
os.path.walk('c:\\src',lister,match)

The second is faster. They are both decent. Thanks again. 

Signed plerplexed and embarrassed.



More information about the Python-list mailing list