This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(module fact) | |
(define *num-list* '(1 12 123 1234 12345 123456)) | |
(define (factorial n) | |
(define (iter k acc) | |
(if (= k 1) | |
acc | |
(iter (- k 1) (* k acc)))) | |
(iter n 1)) | |
(define (dfactorial n) | |
(do ((r 1) | |
(i 1 (+ i 1))) | |
((= i n) r) | |
(set! r (* r i)))) | |
(define (sum-digits n) | |
(reduce + 0 (map | |
(lambda (c) (- (char->integer c) (char->integer #\0))) | |
(string->list (number->string n))))) | |
;; Taken from example of time function in Bigloo reference | |
(multiple-value-bind (res rtime stime utime) | |
(time | |
(lambda () | |
(reduce + 0 (map sum-digits (map factorial *num-list*))))) | |
(print "Recursive factorial: " (* 0.001 rtime) " sec ")) | |
(multiple-value-bind (res rtime stime utime) | |
(time | |
(lambda () | |
(reduce + 0 (map sum-digits (map dfactorial *num-list*))))) | |
(print "Iterative factorial: " (* 0.001 rtime) " sec ")) |
alexey at daphne in ~/Projects/tests$ bigloo -O3 fact.scm
ld: warning: option -s is obsolete and being ignored
alexey at daphne in ~/Projects/tests$ ./a.out
Recursive factorial: 4.44 sec
Iterative factorial: 3.96 sec
It has only factor of 2 compared to C version.
Aucun commentaire:
Enregistrer un commentaire