On Fri, Oct 2, 2009 at 2:25 PM, Manvendra Bhangui <[email protected]> wrote: > On Fri, Oct 2, 2009 at 2:12 PM, Raman.P <[email protected]> wrote: >> The perl script did the job in split second (1.034 seconds) compared to the > shell script (57 secons) > > Reason - there is no fork/exec system calls being done in the perl script >
Here's some python (<20 lines, no dependencies), with my test input. Timing (in seconds): python fold.py < fold.in > fold.out 0.0 # not unicode aware python fold.py < thirukkural > thirukkural.out 0.0749998092651 # reads/writes utf-8 python fold.py < thirukkural > thirukkural.out 0.0979998111725 Speed it up, or reduce the loc (in the function fold, not the support cruft). Roshan Mathews
def fold(string, limit = 66):
out = []
for string in string.split('\n'):
while len(string) > limit:
left, right = string.rfind(' ', 0, limit), string.find(' ', limit)
i = left if left > -1 else right
if i == -1: break
chunk, string = string[:i], string[i:].strip()
out.append(chunk.strip())
out.append(string.strip())
return '\n'.join(out).encode('utf-8')
if __name__ == '__main__':
import sys, time
start = time.time()
print fold(sys.stdin.read().decode('utf-8'))
print >> sys.stderr, time.time() - start
fold.in
Description: Binary data
_______________________________________________ To unsubscribe, email [email protected] with "unsubscribe <password> <address>" in the subject or body of the message. http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
