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

Steven Atkinson sja at san.rr.com
Fri Aug 9 23:30:07 EDT 2002


The Ruby code also had the compile in the inter loop. It is something like
(left it at work and I'm not looking up the Ruby syntax so this might not be
correct):

require "find"

Find.find( 'c:\\src) do | fname |
   p = Regex.compile( wildcards)
   if p.match(fname)
      puts fname
   end
end

I changed both to do the compile outside the loop. The Ruby is just a tad
quicker (something like 13 seconds to Pythons 15 seconds). The main
difference in the code is that the Python one has to combine the dir and the
filename back into a full path in the lister if there is a filename match.

Interesting enough, the Python one gained the most from do the regex compile
outside the loop. It picked up about 20-30% while Ruby only gained 5-10%. I
also changed it to just do a regex match against the filename and then build
back up the path only when there is a match. So probably was also
significant amount of wasted CPU time.

Still can't explain the intial slowness of my intial 5-6 runs. The Python
code was taking several order of magnitudes longer. The walk code seemed to
be pausing on each directory recursion. You'd see a file listing for a dir
and it'd be quick, and then it'd pause for the next. It was hard to tell if
that was because it was. The test environment did not change.

In any case, though that mystery is not solved, at least I know that Ruby is
about the same speed wise as Ruby.

Thanks again.

"Matt Gerrans" <mgerrans at mindspring.com> wrote in message
news:aj13pl$cti$1 at slb3.atl.mindspring.net...
> Additionally, maybe you can post your Ruby code as well.
>
> When you originally said that Python was slower than Ruby, I was
surprised,
> because I don't think the Ruby interpreter is very optimized at this
point.
> When you posted the Python code with the re.compile() in an inner loop, it
> made more sense.
>






More information about the Python-list mailing list