Carlos
10/13/2015 9:42:00 PM
[Teemu Likonen <tlikonen@iki.fi>, 2015-10-13 23:15]
> Carlos [2015-10-13 19:16:51+02] wrote:
[...]
> > I think it's the fastest (yet).
>
> In SBCL, it seems that my version is still the fastest. :-)
Mine is faster in *my* SBCL (that's why it's mine ;-) but only because I
declare the parameter types; it gets a 3x speedup then.
Yours doesn't need that and accepts more parameters. Declaring the loop
variable as string shaves a 10% more and gives it the advantage (for
the only case I tested).
Here are both, with declarations:
CL-USER> (defun string-ior (a b)
(declare (string a b)
(optimize (speed 3)))
(map 'bit-vector (lambda (a b)
(logior (- (char-code a) (char-code #\0))
(- (char-code b) (char-code #\0))))
a b))
STYLE-WARNING: redefining COMMON-LISP-USER::STRING-IOR in DEFUN
STRING-IOR
CL-USER> (defun string-or (&rest strings)
(declare (optimize (speed 3)))
(loop :with lengths := (mapcar #'length strings)
:with max-length := (reduce #'max lengths)
:with bit-array := (make-array max-length :element-type 'bit)
:for i :from 0 :below max-length
:do (setf (aref bit-array (- max-length i 1))
(loop :for string :of-type string :in strings
:for length :in lengths
:if (< i length)
:do (ecase (aref string (- length i 1))
(#\1 (return 1))
(#\0))
:finally (return 0)))
:finally (return bit-array)))
STYLE-WARNING: redefining COMMON-LISP-USER::STRING-OR in DEFUN
STRING-OR
CL-USER> (time (dotimes (_ 1000000) (string-ior "01110111000010101" "01110101010011111")))
Evaluation took:
0.970 seconds of real time
0.756000 seconds of total run time (0.756000 user, 0.000000 system)
77.94% CPU
2,727,366,016 processor cycles
303,990,736 bytes consed
NIL
CL-USER> (time (dotimes (_ 1000000) (string-or "01110111000010101" "01110101010011111")))
Evaluation took:
0.956 seconds of real time
0.936000 seconds of total run time (0.936000 user, 0.000000 system)
97.91% CPU
2,689,284,626 processor cycles
127,998,096 bytes consed
NIL
--