On 2018-04-07 20:51, Tianxiang Xiong wrote:
> The following runs nearly instantaneously:
>
> (progn
> (cl-loop for i in (number-sequence 0 130000)
> collect (cons (number-to-string i) i))
> :done)
This expands to the following:
(progn
(cl-block nil
(let* ((#:--cl-var-- (number-sequence 0 130000))
(i nil)
(#:--cl-var-- nil))
(while (consp #:--cl-var--)
(setq i (car #:--cl-var--))
(setq #:--cl-var-- (cons (cons (number-to-string i) i) #:--cl-var--))
(setq #:--cl-var-- (cdr #:--cl-var--)))
(nreverse #:--cl-var--)))
:done)
> This seems to take a long time (didn't wait for it to finish):
>
> (progn
> (cl-loop for i in (number-sequence 0 130000)
> collect (cons (number-to-string i) i) into pairs)
> :done)
Whereas that expands to this:
(progn
(cl-block nil
(let* ((#:--cl-var-- (number-sequence 0 130000))
(i nil)
(pairs nil))
(while (consp #:--cl-var--)
(setq i (car #:--cl-var--))
(setq pairs (nconc pairs (list (cons (number-to-string i) i))))
(setq #:--cl-var-- (cdr #:--cl-var--)))
nil))
:done)
> Is this a known issue? I couldn't find anything in the bug tracker about it.
The second form is quadratic, maybe because user code is allowed to access the accumulation variable during iteration?
It should likely be documented, but it doesn't seem to be ATM.
Clément.