Gnus development mailing list
 help / color / mirror / Atom feed
* catch/throw
@ 1999-01-27 17:51 Lars Magne Ingebrigtsen
  1999-01-27 18:42 ` catch/throw Justin Sheehy
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Lars Magne Ingebrigtsen @ 1999-01-27 17:51 UTC (permalink / raw)


One other stylistic thing I've started doing lately is using
catch/throw.  Like this:

(defun gnus-or (&rest elems)
  "Return non-nil if any of the elements are non-nil."
  (catch 'found
    (while elems
      (when (pop elems)
	(throw 'found t)))))

where I would have written something like the following before:

(defun gnus-or (&rest elems)
  (let (found)
    (while (and elems
                (not found))
      (when (pop elems)
        (setq found t)))
    found))

or

(defun gnus-or (&rest elems)
  (let (found)
    (while elems
      (when (pop elems)
        (setq found t
              elems nil)))
    found))

I don't know.  catch/throw has gives me the visceral thrill of being
able to say "I've found what I'm looking for!  Stop iterating this
instance and return THIS!  Right now!"  But catch/throw is just goto
under another name, isn't it?  And doesn't one frown on goto?

The second version is quite clear with the explicit test for whether
we have found what we're looking for, but is, of course, slow.  The
third is probably fastest of these three, although the logic here
seems a bit unclear at first peek.  (I'm assuming catch sets up an
unwind-protect form, which probably isn't extremely efficient,
although I haven't really benchmarked...)

(I wouldn't bother going on about this is such length, but these sorts 
of loops are so extremely common...)

(In Common Lisp, we have functions that make many of these types of
loops unnecessary, but I'm not allowed to use the cl functions in
Gnus.)

I've now done benchmarking, and the catch/throw thing seems like it's
10% slower than the other two...

Hm.

But perhaps we should be glad that I can't use the cl functions in
Gnus, since

(defun gnus-or (&rest elems)
  (find-if-not 'null elems))

takes 10 times longer to run than the versions at the top.  :-)

-- 
(domestic pets only, the antidote for overdose, milk.)
  larsi@ifi.uio.no * Lars Magne Ingebrigtsen


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

end of thread, other threads:[~1999-02-02 20:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-01-27 17:51 catch/throw Lars Magne Ingebrigtsen
1999-01-27 18:42 ` catch/throw Justin Sheehy
1999-01-27 21:33 ` catch/throw Hrvoje Niksic
1999-01-28  7:32   ` catch/throw Lars Magne Ingebrigtsen
1999-01-28 18:21     ` catch/throw Hrvoje Niksic
1999-01-28 20:16       ` catch/throw Lars Magne Ingebrigtsen
1999-02-01 23:36 ` catch/throw Hallvard B Furuseth
1999-02-02 20:17   ` catch/throw Lars Magne Ingebrigtsen

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 NNTP newsgroup(s).