[racket] translate from Racket to Common Lisp

From: daniel rupistraliz (danielrupistralizavez at yahoo.es)
Date: Sun Nov 4 10:54:51 EST 2012


 racket: 
Welcome to Racket v5.2.1.
> (define (cycle-length n)
    (cond
      [(= n 1)
       1]
      [(odd? n)
       (add1 (cycle-length (add1 (* 3 n))))]
      [(even? n)
       (add1 (cycle-length (/ n 2)))]))

(time (for ([i (in-range 1 1000000)])
          (cycle-length i)))
> 
cpu time: 15016 real time: 15018 gc time: 0

sbcl:

(defun cycle-length(n)
 (cond ((= n 1) 1)
       ((oddp n) (1+ (cycle-length (1+ (* 3 n)))))
       ((evenp n) (1+ (cycle-length (/ n 2))))))


* (time (loop for i from 1 to 1000000 do (cycle-length i)))

Evaluation took:
  6.192 seconds of real time
  6.192387 seconds of total run time (6.192387 user, 0.000000 system)
  100.00% CPU
  14,496,643,427 processor cycles
  0 bytes consed


With optimizations:

(defun cycle-length(n)
  (declare (fixnum n)
	   (ftype (function (fixnum) fixnum) cycle-length)
	   (optimize (speed 3) (safety 0) (compilation-speed 0)))
  (cond ((= n 1) 1)
	((oddp n) (the fixnum (1+ (cycle-length
				   (the fixnum (1+ (the fixnum (* 3 n))))))))
	((evenp n) (cycle-length (the fixnum (/ n 2))))))

CL-USER> (time (loop for i from 1 to (expt 10 6) do (cycle-length i)))
Evaluation took:
  3.515 seconds of real time
  3.524220 seconds of total run time (3.524220 user, 0.000000 system)
  100.26% CPU
  8,228,949,701 processor cycles
  33,008 bytes consed

So racket = 15 seconds
    sbcl  =  6 seconds
sbcl optimized = 3.5 seconds.



Posted on the users mailing list.