unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: cl-dolist, dolist, cl-return,
Date: Wed, 8 Jul 2015 18:49:34 -0700	[thread overview]
Message-ID: <CAOj2CQT442-Uhu==4fSdCNGfZACPo8rpp9fYOrYC1EdP4JGMVg@mail.gmail.com> (raw)
In-Reply-To: <87oajmi6eh.fsf@nl106-137-147.student.uu.se>

Emanuel Berg <embe8573@student.uu.se> wrote:
>> My point was that the re-evaluation part would be
>> just a side-problem: even if your expression is
>> a mere variable (so re-evaluating it is very cheap),
>> the need to use `nth' at each step would force an
>> O(N^2) complexity to this loop.
>
> You mean `nth' is linear, and `dotimes' is linear, so
> the whole thing is linear**2 = quadratic?
>
> But I'm not using nth - probably you misread "neq".
> (Ha! - maybe a reason not to do it just presented
> itself...)
>
> Here is the code, which is linear, O(n), unless
> there is a hidden nth somewhere...

Stefan means that if, given `(dolist (x (list 1 2 3)) ...)', you
evaluated `(list 1 2 3)' on every iteration you would end up doing
something the moral equivalent of

    (setq elt (nth 0 (list 1 2 3))) ;; first iteration
    (setq elt (nth 1 (list 1 2 3))) ;; second iteration

and so on, rather than

    (let ((the-list (list 1 2 3)))
      (setq elt (pop the-list))  ;; first iteration
      (setq elt (pop the-list))) ;; second iteration

The latter case (which is how things really work) is obviously much
more efficient, because the former case needlessly re-walks
the list.

-- 
john



  reply	other threads:[~2015-07-09  1:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-08  0:14 cl-dolist, dolist, cl-return, Emanuel Berg
2015-07-08  0:31 ` John Mastro
2015-07-08  3:09   ` Stefan Monnier
2015-07-08 10:25     ` Emanuel Berg
2015-07-08 14:44       ` Stefan Monnier
2015-07-08 23:19         ` Emanuel Berg
2015-07-09  1:49           ` John Mastro [this message]
2015-07-09 22:00             ` Emanuel Berg
     [not found]             ` <mailman.6636.1436479362.904.help-gnu-emacs@gnu.org>
2015-07-10 18:44               ` Barry Margolin
2015-07-11 18:52                 ` Emanuel Berg
2015-07-10 16:57           ` Stefan Monnier
2015-07-08 10:23   ` Emanuel Berg
     [not found] <mailman.6511.1436314595.904.help-gnu-emacs@gnu.org>
2015-07-08  3:25 ` Pascal J. Bourguignon
2015-07-08 10:17   ` Emanuel Berg
2015-07-08 14:02     ` Drew Adams
2015-07-08 23:10       ` Emanuel Berg
     [not found]       ` <mailman.6588.1436397019.904.help-gnu-emacs@gnu.org>
2015-07-08 23:56         ` Pascal J. Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOj2CQT442-Uhu==4fSdCNGfZACPo8rpp9fYOrYC1EdP4JGMVg@mail.gmail.com' \
    --to=john.b.mastro@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).