|
Page Summary
|
Ужасный киберпространственный имбецил и просто хороший человек предлагает немедленно купить у него картин. Я бы и сам купил, но у меня денег нет. но точно о какой-то любви: Путник, разгадывать устав загадки бытия %hash = map { get_a_key_for($_) => $_ } @array; http://neivid.livejournal.com/190589.ht Дети, никогда не используйте текстовые значения без констант. От этого бывают неожиданности. постричься что ли?
(defun fact (n)
(labels ((fact-rec (n res)
(if (= n 0) res
(fact-rec (1- n) (* res n)))))
(fact-rec n 1)))
(defun fact-naive (n)
(if (= n 0) 1
(* n (fact-naive (1- n)))))
(defun fact-iter (n)
(do ((i 1 (1+ i))
(res 1 (* res i)))
((> i n) res)))
(defun compare-facts (n &optional check-naive)
(gc)
(if check-naive
(progn
(format t "Not tail-optimized~%")
(time (let ((a (fact-naive n))) 1))))
(gc)
(format t "Tail-optimized~%")
(time (let ((a (fact n))) 1))
(gc)
(format t "Iterative~%")
(time (let ((a (fact-iter n))) 1)))
на 100000 тупая рекурсия сказала что идите вы нафиг я работать не буду (compare-facts 100000 nil) Tail-optimized Evaluation took: 30.774 seconds of real time 30.441903 seconds of total run time (28.905807 user, 1.536096 system) [ Run times consist of 6.840 seconds GC time, and 23.602 seconds non-GC time. ] 98.92% CPU 49,062,212,525 processor cycles 9,931,233,336 bytes consed Iterative Evaluation took: 27.781 seconds of real time 27.581723 seconds of total run time (26.301643 user, 1.280080 system) [ Run times consist of 6.092 seconds GC time, and 21.490 seconds non-GC time. ] 99.28% CPU 44,306,461,638 processor cycles 9,029,669,120 bytes consed (compare-facts 10000 t) Not tail-optimized Evaluation took: 0.246 seconds of real time 0.228014 seconds of total run time (0.224014 user, 0.004000 system) [ Run times consist of 0.040 seconds GC time, and 0.189 seconds non-GC time. ] 92.68% CPU 366,546,075 processor cycles 69,580,064 bytes consed Tail-optimized Evaluation took: 0.245 seconds of real time 0.232014 seconds of total run time (0.232014 user, 0.000000 system) [ Run times consist of 0.052 seconds GC time, and 0.181 seconds non-GC time. ] 94.69% CPU 391,160,995 processor cycles 78,592,824 bytes consed Iterative Evaluation took: 0.210 seconds of real time 0.200012 seconds of total run time (0.200012 user, 0.000000 system) [ Run times consist of 0.040 seconds GC time, and 0.161 seconds non-GC time. ] 95.24% CPU 335,864,257 processor cycles 69,587,768 bytes consed
#lang scheme
(define (fact n)
(let fact ([k n] [res 1])
(if (zero? k) res
(fact (sub1 k) (* res k)))))
(define (fact-iter n)
(do ([i 1 (+ i 1)]
[res 1 (* res i)])
((> i n) res)))
(define (fact-naive n)
(if (zero? n) 1
(* n (fact-naive (- n 1)))))
(define (compare-facts n)
(collect-garbage)
(time (let ([a (fact-naive n)]) 1))
(collect-garbage)
(time (let ([a (fact n)]) 1))
(collect-garbage)
(time (let ([a (fact-iter n)]) 1)))
(compare-facts 100000) cpu time: 92778 real time: 98345 gc time: 13456 cpu time: 87638 real time: 90067 gc time: 11701 cpu time: 79981 real time: 80853 gc time: 10196 Поднял на своем серваке жаббер... Читал вчера всякое по программированию. Набрел на замечательную фразу:
|



