Strange Execution Times

John Machin sjmachin at lexicon.net
Fri May 27 07:50:50 EDT 2005


Elliot Temple wrote:
> 
> On May 26, 2005, at 3:22 PM, John Machin wrote:
> 
>>
>> Then post your summarised results back to the newsgroup for the  
>> benefit of all -- there's this vague hope that folk actually read  
>> other peoples' posts before firing off questions :-)
> 
> 
> Here is my new version.  It runs in about .65 seconds.  The trick?   
> Reading lines one at a time.  Please let me know if there's any bad  
> coding practices in it!
> 
> 

>     for line in f:
>         start, end = line.find(p1) + adjust, line.find(p2)
>         if end != -1:
>             digest = md5.new(line[start:end]).hexdigest()
>             out.write(line[:start] + digest + line[end:])
>         else:
>             out.write(line)
> 

Hmmm ... simple, elegant *and* runs fast!

Only two minor points:

1. Your code assumes that there can be no more than one password per 
line and that there are no "syntax errors". I'd add at least a comment 
to that effect.

2. The scan for p2 is wasted if the scan for p1 finds nothing. If p1 is 
found, you scan for p2 from the beginning of the line. Depending on the 
average length of a line, etc, that could make a difference. Try this:

for line in f:
     start = line.find(p1)
     if start == -1:
         out.write(line)
     else:
         start += adjust
         end = line.find(p2, start)
         if end == -1:
             raise CannotHappenError
         digest = md5.new(line[start:end]).hexdigest()
         out.write(line[:start] + digest + line[end:])

Cheers,
John



More information about the Python-list mailing list