Deutsch   English   Français   Italiano  
<87h6bwrufd.fsf@nightsong.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Paul Rubin <no.email@nospam.invalid>
Newsgroups: comp.lang.lisp
Subject: Optimization flag for unchecked fixnums in SBCL?
Date: Wed, 07 Aug 2024 12:42:46 -0700
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <87h6bwrufd.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 07 Aug 2024 21:42:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="474d0916c39bc011b73c3c05ef9382fb";
	logging-data="3487170"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18+3zCI2ugN5y7uCLe72kgr"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:PiY1apd9Q2R+eTxSnNhtoYDuFlk=
	sha1:L5NgduQCrANnZ/MKrMNAvaRHecc=
Bytes: 2092

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)