[alexandria-devel] Optimization for whichever

Gustavo gugamilare at gmail.com
Wed Mar 3 14:27:01 UTC 2010


Hello,

I have optimized the expansion of the macro whichever. In the current
implementation, it generates n random values, creates O(n) functions and
calls one of them. Using a sort of "inline" binary search, I was able to
create an expansion which makes one call to RANDOM, O(log n) tests, and also
does not create any extra function.

A test with my implementation:

cl-user> (macroexpand-1 '(whichever (list 1) (list 2) (list 3) (list 4)
(list 5) (list 6) (list 7)))
(let ((#:random-number854 (random 7)))
  (if (< #:random-number854 3)
      (if (< #:random-number854 1)
          (list 1)
          (if (< #:random-number854 2)
              (list 2)
              (list 3)))
      (if (< #:random-number854 5)
          (if (< #:random-number854 4)
              (list 4)
              (list 5))
          (if (< #:random-number854 6)
              (list 6)
              (list 7)))))
t

Regards,
Gustavo Henrique Milaré.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/alexandria-devel/attachments/20100303/a6649cac/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: control-flow.patch
Type: text/x-diff
Size: 1755 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/alexandria-devel/attachments/20100303/a6649cac/attachment.patch>


More information about the alexandria-devel mailing list