unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#7174: 23.2; `last' can be made faster by using `length'
@ 2010-10-08  1:31 IRIE Shinsuke
  2010-10-13  3:30 ` Glenn Morris
  0 siblings, 1 reply; 14+ messages in thread
From: IRIE Shinsuke @ 2010-10-08  1:31 UTC (permalink / raw)
  To: 7174

Recently I saw the implementation of `last' in subr.el and found that
it counts the number of list elements on its own:

(defun last (list &optional n)
  "Return the last link of LIST.  Its car is the last element.
If LIST is nil, return nil.
If N is non-nil, return the Nth-to-last link of LIST.
If N is bigger than the length of LIST, return LIST."
  (if n
      (let ((m 0) (p list))
	(while (consp p)
	  (setq m (1+ m) p (cdr p)))
	(if (<= n 0) p
	  (if (< n m) (nthcdr (- m n) list) list)))
    (while (consp (cdr list))
      (setq list (cdr list)))
    list))

So I modified the code to use `length' as follows, and confirmed
it becomes much faster:

(defun last (list &optional n)
  "Return the last link of LIST.  Its car is the last element.
If LIST is nil, return nil.
If N is non-nil, return the Nth-to-last link of LIST.
If N is bigger than the length of LIST, return LIST."
  (if n
      (and (> n 0)
	   (let ((m (length list)))
	     (if (< n m) (nthcdr (- m n) list) list)))
    (and list
	 (nthcdr (1- (length list)) list))))

Furthermore, the code can be made much simpler (but slower than
the above, in particular cases) as:

(defun last (list &optional n)
  "Return the last link of LIST.  Its car is the last element.
If LIST is nil, return nil.
If N is non-nil, return the Nth-to-last link of LIST.
If N is bigger than the length of LIST, return LIST."
  (nthcdr (- (length list) (or n 1)) list))

Thanks.

IRIE Shinsuke





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-08  1:31 bug#7174: 23.2; `last' can be made faster by using `length' IRIE Shinsuke
@ 2010-10-13  3:30 ` Glenn Morris
  2010-10-13 22:25   ` Juanma Barranquero
  0 siblings, 1 reply; 14+ messages in thread
From: Glenn Morris @ 2010-10-13  3:30 UTC (permalink / raw)
  To: 7174-done


Thanks; applied.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13  3:30 ` Glenn Morris
@ 2010-10-13 22:25   ` Juanma Barranquero
  2010-10-13 22:48     ` Glenn Morris
  2010-10-13 23:00     ` IRIE Shinsuke
  0 siblings, 2 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-10-13 22:25 UTC (permalink / raw)
  To: 7174, rgm; +Cc: 7174-done

> Thanks; applied.

They are not equivalent:

(last '(1 . 2) 0)  => 2
(last-new '(1 . 2) 0) => nil

    Juanma





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 22:25   ` Juanma Barranquero
@ 2010-10-13 22:48     ` Glenn Morris
  2010-10-13 22:52       ` Juanma Barranquero
  2010-10-13 23:00     ` IRIE Shinsuke
  1 sibling, 1 reply; 14+ messages in thread
From: Glenn Morris @ 2010-10-13 22:48 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: 7174

Juanma Barranquero wrote:

> They are not equivalent:
>
> (last '(1 . 2) 0)  => 2
> (last-new '(1 . 2) 0) => nil

See also http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7206

(defun last-new2 (list &optional n)
  "Return the last link of LIST.  Its car is the last element.
If LIST is nil, return nil.
If N is non-nil, return the Nth-to-last link of LIST.
If N is bigger than the length of LIST, return LIST."
  (if n
      (and (>= n 0)
         (let ((m (safe-length list)))
              (if (< n m) (nthcdr (- m n) list) list)))
    (and list
     (nthcdr (1- (safe-length list)) list))))


(last-new2 '(1 . 2) 0) => 2





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 22:48     ` Glenn Morris
@ 2010-10-13 22:52       ` Juanma Barranquero
  2010-10-13 22:56         ` Glenn Morris
  0 siblings, 1 reply; 14+ messages in thread
From: Juanma Barranquero @ 2010-10-13 22:52 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 7174

On Thu, Oct 14, 2010 at 00:48, Glenn Morris <rgm@gnu.org> wrote:

> See also http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7206

Yes, I was seeing that same error.

> (last-new2 '(1 . 2) 0) => 2

Fine. Are you going to install it?

Thanks,

    Juanma





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 22:52       ` Juanma Barranquero
@ 2010-10-13 22:56         ` Glenn Morris
  2010-10-13 23:16           ` Juanma Barranquero
  0 siblings, 1 reply; 14+ messages in thread
From: Glenn Morris @ 2010-10-13 22:56 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: 7174

Juanma Barranquero wrote:

> Fine. Are you going to install it?

Can't for several hours, so if you want to, feel free.






^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 22:25   ` Juanma Barranquero
  2010-10-13 22:48     ` Glenn Morris
@ 2010-10-13 23:00     ` IRIE Shinsuke
  2010-10-13 23:30       ` Glenn Morris
  1 sibling, 1 reply; 14+ messages in thread
From: IRIE Shinsuke @ 2010-10-13 23:00 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: 7174, 7174-done

Oops, sorry, I should've considered more...

IRIE Shinsuke





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 22:56         ` Glenn Morris
@ 2010-10-13 23:16           ` Juanma Barranquero
  2010-10-13 23:29             ` Glenn Morris
  2010-10-13 23:29             ` IRIE Shinsuke
  0 siblings, 2 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-10-13 23:16 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 7174

On Thu, Oct 14, 2010 at 00:56, Glenn Morris <rgm@gnu.org> wrote:

> Can't for several hours, so if you want to, feel free.

Done.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 23:16           ` Juanma Barranquero
@ 2010-10-13 23:29             ` Glenn Morris
  2010-10-13 23:44               ` Juanma Barranquero
  2010-10-13 23:29             ` IRIE Shinsuke
  1 sibling, 1 reply; 14+ messages in thread
From: Glenn Morris @ 2010-10-13 23:29 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: 7174

Juanma Barranquero wrote:

> Done.

Cheers.

My "(>= n 0)" bit, which seems to fix your particular example from
this report, is still outstanding.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 23:16           ` Juanma Barranquero
  2010-10-13 23:29             ` Glenn Morris
@ 2010-10-13 23:29             ` IRIE Shinsuke
  1 sibling, 0 replies; 14+ messages in thread
From: IRIE Shinsuke @ 2010-10-13 23:29 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: 7174

At Thu, 14 Oct 2010 01:16:25 +0200,
Juanma Barranquero wrote:
 
> Done.

Thanks.

IRIE Shinsuke





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 23:00     ` IRIE Shinsuke
@ 2010-10-13 23:30       ` Glenn Morris
  2010-10-13 23:45         ` Juanma Barranquero
  2010-10-14  4:35         ` IRIE Shinsuke
  0 siblings, 2 replies; 14+ messages in thread
From: Glenn Morris @ 2010-10-13 23:30 UTC (permalink / raw)
  To: IRIE Shinsuke; +Cc: Juanma Barranquero, 7174

IRIE Shinsuke wrote:

> Oops, sorry, I should've considered more...

No worries. However long this sat uncommitted, it was inevitable
somebody would find some issue right after it was installed.

(I assume it is still faster than the original, BTW?)





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 23:29             ` Glenn Morris
@ 2010-10-13 23:44               ` Juanma Barranquero
  0 siblings, 0 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-10-13 23:44 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 7174

On Thu, Oct 14, 2010 at 01:29, Glenn Morris <rgm@gnu.org> wrote:

> My "(>= n 0)" bit, which seems to fix your particular example from
> this report, is still outstanding.

Yes, an oversight, sorry. Fixed now.

    Juanma





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 23:30       ` Glenn Morris
@ 2010-10-13 23:45         ` Juanma Barranquero
  2010-10-14  4:35         ` IRIE Shinsuke
  1 sibling, 0 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-10-13 23:45 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 7174

On Thu, Oct 14, 2010 at 01:30, Glenn Morris <rgm@gnu.org> wrote:

> No worries. However long this sat uncommitted, it was inevitable
> somebody would find some issue right after it was installed.

How true. The best way to find bugs in a patch is under fire.

> (I assume it is still faster than the original, BTW?)

That's a good question.

    Juanma





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#7174: 23.2; `last' can be made faster by using `length'
  2010-10-13 23:30       ` Glenn Morris
  2010-10-13 23:45         ` Juanma Barranquero
@ 2010-10-14  4:35         ` IRIE Shinsuke
  1 sibling, 0 replies; 14+ messages in thread
From: IRIE Shinsuke @ 2010-10-14  4:35 UTC (permalink / raw)
  To: Glenn Morris; +Cc: Juanma Barranquero, 7174

At Wed, 13 Oct 2010 19:30:55 -0400,
Glenn Morris wrote:

> (I assume it is still faster than the original, BTW?)

I examined that.

The new `last' is still faster than the original even though it uses
`safe-length', except for particular cases as follows:

  (last nil 0)   (last '(foo) 0)   (last '(foo))

Here, '(foo) is a list of one element. In these cases, it's 2-5% slower.
If LIST has 1000 elements, 4-8 times faster.

IRIE Shinsuke





^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2010-10-14  4:35 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-08  1:31 bug#7174: 23.2; `last' can be made faster by using `length' IRIE Shinsuke
2010-10-13  3:30 ` Glenn Morris
2010-10-13 22:25   ` Juanma Barranquero
2010-10-13 22:48     ` Glenn Morris
2010-10-13 22:52       ` Juanma Barranquero
2010-10-13 22:56         ` Glenn Morris
2010-10-13 23:16           ` Juanma Barranquero
2010-10-13 23:29             ` Glenn Morris
2010-10-13 23:44               ` Juanma Barranquero
2010-10-13 23:29             ` IRIE Shinsuke
2010-10-13 23:00     ` IRIE Shinsuke
2010-10-13 23:30       ` Glenn Morris
2010-10-13 23:45         ` Juanma Barranquero
2010-10-14  4:35         ` IRIE Shinsuke

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).