[scheme] рекурсия против итерации
Форум — Development
Привет все. Тут такое дело, чего то я недопонимаю ...
Имеется две реализации !
% cat tttt.scm
#!/usr/bin/env gsi-script
(define factorial-1
(lambda (n)
(if (zero? n)
1
(* n (factorial-1 (- n 1))))))
(define factorial-2
(lambda (n)
(let iteration ((i n)
(p 1))
(if (zero? i)
p
(iteration (- i 1) (* p i))))))
(time (factorial-1 16384))
(newline)
(time (factorial-2 16384))
Причем вторая работает медленнее, хотя я ожидал что она будет
быстрее.
% ./tttt.scm
(time (factorial-1 16384))
1376 ms real time
1297 ms cpu time (891 user, 406 system)
139 collections accounting for 417 ms real time (273 user, 141 system)
206481088 bytes allocated
62384 minor faults
no major faults
(time (factorial-2 16384))
1844 ms real time
1734 ms cpu time (1242 user, 492 system)
762 collections accounting for 818 ms real time (625 user, 141 system)
228843664 bytes allocated
67161 minor faults
no major faults
Как же tail call optimisation и все такое? Поясните если можно.