help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: return first element in list with certain property


From: Eric Abrahamsen
Subject: Re: return first element in list with certain property
Date: Mon, 20 Nov 2017 14:02:05 -0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Emanuel Berg <moasen@zoho.com> writes:

> Eric Abrahamsen wrote:
>
>> FWIW, this is essentially how seq-find works,
>> and I generally use this.
>
> Here (my use case) it is important that the
> search is linear along the list from the
> beginning as the hit is almost always in the
> very early section of the list.

In that case there will be less difference between the two: as Drew
noted, `cl-find-if' will traverse the list twice, which only becomes a
big problem if the desired element is at the end of the list.

But enough speculation!

(setq test-front (append '(1 1 3) (make-list 1000 1)))

(setq test-back (append (make-list 1000 1) '(3 1 1)))

(benchmark 1000 '(seq-find (lambda (elt) (= 3 elt)) test-front))
"Elapsed time: 0.008247s"

(benchmark 1000 '(cl-find-if (lambda (elt) (= 3 elt)) test-front))
"Elapsed time: 0.006165s"

(benchmark 1000 '(seq-find (lambda (elt) (= 3 elt)) test-back))
"Elapsed time: 0.134623s"

(benchmark 1000 '(cl-find-if (lambda (elt) (= 3 elt)) test-back))
"Elapsed time: 0.167007s"

Yup, in your case it doesn't matter too much.

Eric




reply via email to

[Prev in Thread] Current Thread [Next in Thread]