In sintesi,
>>> :set +s -- activate performance
>>> :r -- reset all interactive binding
Ok, modules loaded: none.
>>> :show bindings -- check the binding state
Cominciamo il test,
>>> let f = 1 : map (2*) f
(0.01 secs, 1543272 bytes)
>>> :show bindings
f :: [Integer] = _
>>> last $ show $ f !! 50000
'6'
(0.55 secs, 170011128 bytes)
>>> :show bindings
f :: [Integer] = 1 : 2 : 4 : 8 : 16 : ....
it :: Char = '6'
>>> last $ show $ f !! 50000
'6'
(0.02 secs, 1562456 bytes)
Utilizzando indefinito,
>>> let f = undefined
(0.01 secs, 565912 bytes)
>>> :show bindings
it :: Char = '6'
f :: a = _
>>> let f = 1 : map (2*) f
(0.01 secs, 513304 bytes)
>>> last $ show $ f !! 50000
'6'
(0.94 secs, 170517840 bytes)
>>> :show bindings
f :: [Integer] = 1 : 2 : 4 : 8 : 16 : ....
it :: Char = '6'
resettare il legame,
>>> :r
>>> :show bindings
Ok, modules loaded: none.
Un altro caso studie,
>>> let h = (2*)
(0.01 secs, 590232 bytes)
>>> let f = 1 : map h f
(0.01 secs, 1138792 bytes)
>>> :show bindings
it :: Char = '6'
h :: Integer -> Integer = _
f :: [Integer] = _
>>> last $ show $ f !! 60000
'6'
(1.69 secs, 241802432 bytes)
>>> last $ show $ f !! 60000
'6'
(0.03 secs, 2002432 bytes)
ancora nella cache, modificare il legame di h di vedere,
>>> let h = (3*)
(0.01 secs, 547208 bytes)
>>> last $ show $ f !! 60000
'6'
(0.03 secs, 2029592 bytes)
>>> :show bindings
f :: [Integer] = 1 : 2 : 4 : 8 : 16 : ....
h :: Integer -> Integer = _
it :: Char = '6'
fare, non importa, hanno bisogno di ridefinire f anche,
>>> let f = 1 : map h f
(0.01 secs, 552048 bytes)
>>> last $ show $ f !! 60000
'1'
(4.36 secs, 374064760 bytes)
Utilizzo Let .. in ... binding,
>>> let f = let h = (2*) in 1 : map h f
(0.02 secs, 1068272 bytes)
>>> last $ show $ f !! 60000
'6'
(3.90 secs, 242190168 bytes)
>>> last $ show $ f !! 60000
'6'
(4.89 secs, 242271560 bytes)
>>> last $ show $ f !! 60000
'6'
(5.71 secs, 242196976 bytes)
>>> :show bindings
h :: Integer -> Integer = _
f :: Num a => [a] = _
it :: Char = '6'
Potrebbe fornire un esempio di una funzione specifica chiamata che viene memorizzata nella cache, per favore? – dave4420
'' let f = 1: map (2 *) f'' e quindi '' last $ show $ f !! 200000''. La prima volta, la macchina richiede circa 15 secondi, ma viene calcolata immediatamente la seconda volta. – user42179
Questa non è una funzione. –