[Openmcl-devel] Why is with-output-to-string so slow?

Ron Garret ron at flownet.com
Wed Oct 27 21:28:32 UTC 2010


WITH-OUTPUT-TO-STRING seems to have an inordinate amount of overhead.  Baseline benchmark:

? (setf devnull (open "/dev/null" :direction :output :if-exists :overwrite))
#<BASIC-CHARACTER-OUTPUT-STREAM ISO-8859-1 (CHARACTER-SPECIAL/12) #x30200593DFCD>
? (time (dotimes (i 1000000) (princ i devnull)))
;Compiler warnings :
;   In an anonymous lambda form at position 36: Undeclared free variable DEVNULL
(DOTIMES (I 1000000) (PRINC I DEVNULL)) took 1,054,338 microseconds (1.054338 seconds) to run 


Capturing the output in strings is almost four times slower:

? (time (dotimes (i 1000000) (with-output-to-string (s) (princ i s))))
(DOTIMES (I 1000000) (WITH-OUTPUT-TO-STRING (S) (PRINC I S))) took 3,952,464 microseconds (3.952464 seconds) to run 


Simply allocating the extra storage does not account for the difference:

? (defun foo () (make-array 10 :element-type 'character :fill-pointer t :adjustable t))
FOO
? (time (dotimes (i 1000000) (foo) (princ i devnull)))
;Compiler warnings :
;   In an anonymous lambda form at position 42: Undeclared free variable DEVNULL
(DOTIMES (I 1000000) (FOO) (PRINC I DEVNULL)) took 1,453,295 microseconds (1.453295 seconds) to run 


What accounts for all the extra overhead?  Is there any way to speed it up?

Thanks,
rg




More information about the Openmcl-devel mailing list