Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,5 +1,6 @@
|
|||
proc ifact_caching n {
|
||||
global fact_cache
|
||||
tailcall fact [expr {$n-1}] [expr {$n*$result}]
|
||||
if { ! [info exists fact_cache]} {
|
||||
set fact_cache {1 1}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,52 @@
|
|||
puts [ifact 30]
|
||||
puts [rfact 30]
|
||||
puts [ifact_caching 30]
|
||||
# calls cmd with args
|
||||
# retpeatedly in scope above
|
||||
proc trampoline {cmd args} {
|
||||
|
||||
set n 400
|
||||
set iterations 10000
|
||||
puts "calculate $n factorial $iterations times"
|
||||
puts "ifact: [time {ifact $n} $iterations]"
|
||||
puts "rfact: [time {rfact $n} $iterations]"
|
||||
# for the caching proc, reset the cache between each iteration so as not to skew the results
|
||||
puts "ifact_caching: [time {ifact_caching $n; unset -nocomplain fact_cache} $iterations]"
|
||||
# thunk is {cmd {arg1 arg2 arg3 ...} }
|
||||
set result [uplevel 1 [concat $cmd $args]]
|
||||
|
||||
# split into vars
|
||||
lassign $result type thunk
|
||||
|
||||
# loop
|
||||
while {$type eq "next"} {
|
||||
set result [uplevel 1 $thunk]
|
||||
lassign $result type thunk
|
||||
}
|
||||
|
||||
set final_value $thunk
|
||||
|
||||
return $final_value
|
||||
}
|
||||
|
||||
# return { value final_value }
|
||||
# or { next {cmd arg1 arg2} }
|
||||
proc factorial_step {n result} {
|
||||
|
||||
if {$n <= 1} {
|
||||
set ret_value [list "value" $result]
|
||||
} else {
|
||||
|
||||
# n-1
|
||||
set next_n [expr {$n-1}]
|
||||
|
||||
# (n-1) * fact(n)
|
||||
set next_result [expr {$n * $result}]
|
||||
|
||||
# {func arg1 arg2}
|
||||
set next_thunk [list factorial_step $next_n $next_result]
|
||||
|
||||
# Return the next step as a list
|
||||
set ret_value [list "next" $next_thunk]
|
||||
}
|
||||
|
||||
return $ret_value
|
||||
}
|
||||
|
||||
|
||||
# The execution is wrapped by the trampoline
|
||||
proc factorial {n} {
|
||||
return [trampoline factorial_step $n 1]
|
||||
}
|
||||
|
||||
set f [factorial 100]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
package require math::special
|
||||
puts [ifact 30]
|
||||
puts [rfact 30]
|
||||
puts [ifact_caching 30]
|
||||
|
||||
proc gfact n {
|
||||
expr {round([::math::special::Gamma [expr {$n+1}]])}
|
||||
}
|
||||
set n 400
|
||||
set iterations 10000
|
||||
puts "calculate $n factorial $iterations times"
|
||||
puts "ifact: [time {ifact $n} $iterations]"
|
||||
puts "rfact: [time {rfact $n} $iterations]"
|
||||
# for the caching proc, reset the cache between each iteration so as not to skew the results
|
||||
puts "ifact_caching: [time {ifact_caching $n; unset -nocomplain fact_cache} $iterations]"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue