fork download
  1. (in-package "CL-USER")
  2.  
  3. (defun string-reverse (string)
  4. (declare (type simple-string string)
  5. (optimize (speed 3)))
  6. (check-type string simple-string)
  7. (loop with len = (length string)
  8. with s = (make-string len)
  9. for i from 0
  10. for j from (1- len) downto 0
  11. do (setf (char s i)
  12. (char string j))
  13. finally (return s)))
  14.  
  15. (compile 'string-reverse)
  16.  
  17. (assert (equal "" (string-reverse "")))
  18. (assert (equal "a" (string-reverse "a")))
  19. (assert (equal "ba" (string-reverse "ab")))
  20. (assert (equal "cba" (string-reverse "abc")))
  21. (assert (equal "dcba" (string-reverse "abcd")))
  22. (assert (equal "edcba" (string-reverse "abcde")))
  23. (let ((s "abcdef")) (assert (not (eq s (string-reverse s)))))
  24.  
  25. (let ((N 1000000)
  26. (input "いろはにほへと"))
  27. (dolist (fn '(STRING-REVERSE
  28. COMMON-LISP:REVERSE))
  29. (time (loop initially (format t "~&~A:~A~%"
  30. (package-name (symbol-package fn))
  31. fn)
  32. with s = (copy-seq input)
  33. for r = (funcall fn s)
  34. repeat N
  35. finally (format t "~S -> ~S~%" input r)))
  36. #+clisp (terpri)
  37. #-sbcl (terpri)))
  38.  
  39. #+ccl (quit)
  40.  
Success #stdin #stdout 4.48s 10500KB
stdin
Standard input is empty
stdout
COMMON-LISP-USER:STRING-REVERSE
"いろはにほへと" -> "とへほにはろい"
Real time: 2.780737 sec.
Run time: 2.772 sec.
Space: 88013896 Bytes
GC: 98, GC time: 0.128 sec.

COMMON-LISP:REVERSE
"いろはにほへと" -> "とへほにはろい"
Real time: 1.698708 sec.
Run time: 1.692 sec.
Space: 56013768 Bytes
GC: 62, GC time: 0.068 sec.