Re: Is this too inefficient?

From: Andrew Koenig <>
Date: Sat, 13 Jan 1996 17:40:19 EST

>> For an n-line file this does n mallocs each of which is an average of
>> half the length of the file, and the number of characters copied in
>> calls to string-append is half the square of the length of the file.
>> Does this seem unreasonably inefficient to anyone else out there?
Ken> A recent C++ Report article by Andrew Koenig (Jan 96 i think) on C++
Ken> performance uses something like this bug as an example. His solution is
Ken> similar to Lars':
Ken> (define (port->string p)
Ken> (unless (or (input-port? p) (input-string-port? p))
Ken> (error "port->string: Bad port ~S" p))
Ken> ;; Read all the lines of port and put them in a string
Ken> (let loop ((res '()))
Ken> (let ((line (read-line p))) ; aesthetic change only
Ken> (if (eof-object? line)
Ken> (apply string-append (reverse! res))
Ken> (loop (cons "\n" (cons line res)))))))
Well, I didn't exactly publish a program in Lisp!

If I remember correctly, the problem I was discussing was that of
forming a string consisting of n concatenations of a given string.
Received on Sat Jan 13 1996 - 23:44:30 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST