Optimization flag for unchecked fixnums in SBCL?

Liste des GroupesRevenir à cl lisp 
Sujet : Optimization flag for unchecked fixnums in SBCL?
De : no.email (at) *nospam* nospam.invalid (Paul Rubin)
Groupes : comp.lang.lisp
Date : 07. Aug 2024, 21:42:46
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87h6bwrufd.fsf@nightsong.com>
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
I looked in the manual and didn't see a way to do this.  The following
Haskell code (Euler problem 14) takes about 0.5 seconds on my old laptop:

    cl :: Int -> Int
    cl n = if odd n then 3*n+1 else n`quot`2
    dl n = (1+) . length . takeWhile (/= 1) . iterate cl $ n
    main = print . maximum $ [(dl n, n) | n <- [1..1000000]]

Int is Haskell's built-in fixnum type.  Integer is bignums and that
version takes about 7 seconds.

The below CL version takes 5 seconds (this is chopped down from a
memoized version that takes about 0.2 seconds).  I tried a few different
ways to tell the compiler that `collatz' should take and return fixnums,
but I didn't find the right way.  Any help?  Thanks.

    (defun collatz (n)
      (cond ((oddp n) (1+ (* 3 n)))
            (t (floor n 2))))

    (defun clen (n)
      (cond ((= n 1) 1)
            ((<= n 0) 'crash)
            (t (1+ (clen (collatz n))))))

    (defun run (&optional (n 1) (mi 0) (ma 0))
        (if (> n 1000000)
          (list mi ma)
          (let ((a (clen n))
                (nn (1+ n)))
            (if (> a ma)
                (run nn n a)
                (run nn mi ma)))))
    (print (run))
    (terpri)

Date Sujet#  Auteur
7 Aug 24 * Optimization flag for unchecked fixnums in SBCL?9Paul Rubin
8 Aug 24 +* Re: Optimization flag for unchecked fixnums in SBCL?5Jeff Barnett
8 Aug 24 i+* Re: Optimization flag for unchecked fixnums in SBCL?2Kaz Kylheku
8 Aug 24 ii`- Re: Optimization flag for unchecked fixnums in SBCL?1Jeff Barnett
10 Aug 24 i`* Re: Optimization flag for unchecked fixnums in SBCL?2Paul Rubin
11 Aug 24 i `- Re: Optimization flag for unchecked fixnums in SBCL?1David De La Harpe Golden
8 Aug 24 +- Re: Optimization flag for unchecked fixnums in SBCL?1David De La Harpe Golden
11 Aug 24 `* Re: Optimization flag for unchecked fixnums in SBCL?2steve g
13 Aug 24  `- Re: Optimization flag for unchecked fixnums in SBCL?1Paul Rubin

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal