Low performance of (concatenate 'string ...).

Bug #2066039 reported by Deiv Astra
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Invalid
Undecided
Unassigned

Bug Description

Hello, I have not an idea why builtin (concatenate 'string ) so slow, I am newbie in CL.
I had test string stream vs concatenate performance and got results below:

"Test string output stream"
189993
Evaluation took:
  0.013 seconds of real time
  0.012293 seconds of total run time (0.008819 user, 0.003474 system)
  92.31% CPU
  39,345,984 processor cycles
  6,588,160 bytes consed

"Test concatenate"
190008
Evaluation took:
  3.480 seconds of real time
  3.464029 seconds of total run time (3.059003 user, 0.405026 system)
  [ Real times consist of 0.280 seconds GC time, and 3.200 seconds non-GC time. ]
  [ Run times consist of 0.289 seconds GC time, and 3.176 seconds non-GC time. ]
  99.54% CPU
  11,123,607,584 processor cycles
  38,009,081,712 bytes consed

=== Test code ===

(defconstant *count* 100000)

(print "Test string output stream")
(defparameter out-stream (make-string-output-stream))
(time
   (progn
      (dotimes (i *count*)
         (write-string (write-to-string (random 100)) out-stream))
      (let (
            (result (get-output-stream-string out-stream)))
         (print (length result))
      )))

(print "Test concatenate")
(defparameter out-string nil)
(time
   (progn
      (dotimes (i *count*)
         (setq out-string
            (concatenate 'string out-string (write-to-string (random 100)))))
      (print (length out-string))
   ))

-----
The result above displays how (make-string-output-stream) is much fastest than (concatenate 'string).
I had write own simple implementation (concatenate-string)

(defun concatenate-string (&rest args)
   (let ((stream (make-string-output-stream)))
      (dolist (arg args)
         (write-string
            (if (stringp arg)
               arg
               (write-to-string arg))
     stream))
 (get-output-stream-string stream)))

This issue published for help to improve performance of SBCL.
Maybe using (make-string-output-stream) inside of (concatenate 'string) will increase performance greatly.
Thanks.

Deiv Astra (deiv-astra)
description: updated
description: updated
Revision history for this message
Douglas Katzman (dougk) wrote :

Your tests are not at all equivalent. In the "Test concatenate" example you are producing as many intermediate strings as there are loop iterations, and each intermediate string is a newly made string containing the entire contents of all prior loop iterations.

Changed in sbcl:
status: New → Won't Fix
Stas Boukarev (stassats)
Changed in sbcl:
status: Won't Fix → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.