[plt-scheme] reverse operation - how fast?

From: Doug Orleans (dougo at place.org)
Date: Fri Apr 8 10:28:48 EDT 2005

Hans Oesterholt-Dijkema writes:
 > Could someone tell me what the performance impact
 > is of the 'reverse' primitive in the code beneath:
 > 
 > *********************************************
 > (define (get-element-from-fifo fifo)
 >   (let ((elem (car (reverse (car fifo)))))
 >     (set-car! fifo (reverse (cdr (reverse (car fifo)))))
 >     elem))
 > *********************************************

Pretty bad, you're copying the list 3 times.  Consider using
take-right and drop-right from SRFI 1, or split-at, or write your own
split-at-right (beats me why this isn't in the SRFI already) that only
traverses the list once.  And maybe use the linear-update variants if
performance is really critical.

Or just use the queue implementation from PLaneT's "galore" package.

--dougo at place.org



Posted on the users mailing list.