Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun padded-string (padding n &optional from-end)
- (declare (optimize speed)
- (type string padding))
- (with-output-to-string (s)
- (loop :for i fixnum :below n
- :with padding := (if from-end (reverse padding) padding)
- :do (princ (char padding (mod i (length padding))) s))))
- (defun pad-with-string (n str &key (from-end nil))
- (declare (optimize speed)
- (type string str))
- (labels ((circular-list (elements)
- (let ((cycle (copy-list elements)))
- (nconc cycle cycle)))
- (maybe-reverse (thing)
- ;; cannot optimize here because `maybe-reverse'
- ;; takes either string or list
- (if from-end (reverse thing) thing)))
- (coerce (the list (maybe-reverse
- (the list (loop repeat n
- for c character in
- (circular-list (coerce (the string (maybe-reverse str)) 'list))
- collect c))))
- 'string)))
- (defun test-padded-string ()
- (declare (optimize speed))
- (loop :repeat 100
- :do (padded-string (make-string 1000 :initial-element #\x) 100)))
- (defun test-pad-with-string ()
- (declare (optimize speed))
- (loop :repeat 100
- :do (pad-with-string 100 (make-string 1000 :initial-element #\x))))
- (defun print-test-statistics ()
- (time (test-padded-string))
- (gc)
- (time (test-pad-with-string)))
- CL-USER> (print-test-statistics)
- Evaluation took:
- 0.002 seconds of real time
- 0.002000 seconds of total run time (0.002000 user, 0.000000 system)
- 100.00% CPU
- 4,661,395 processor cycles
- 502,288 bytes consed
- Evaluation took:
- 0.002 seconds of real time
- 0.002000 seconds of total run time (0.002000 user, 0.000000 system)
- 100.00% CPU
- 4,911,066 processor cycles
- 3,788,512 bytes consed
- NIL
- CL-USER> (print-test-statistics)
- Evaluation took:
- 0.002 seconds of real time
- 0.002000 seconds of total run time (0.002000 user, 0.000000 system)
- 100.00% CPU
- 4,621,939 processor cycles
- 532,752 bytes consed
- Evaluation took:
- 0.002 seconds of real time
- 0.002000 seconds of total run time (0.002000 user, 0.000000 system)
- 100.00% CPU
- 4,962,756 processor cycles
- 3,788,368 bytes consed
- NIL
Advertisement
Add Comment
Please, Sign In to add comment