我有这个代码用于计算硬币的最小数量以进行更改。一个是非递归版本,由函数change
调用,递归回溯函数调用get_min_coin_configuration
。在后一个函数中,我缓存了之前计算过的结果。我认为应该加快这个过程。但实际上,它比不缓存结果的非递归的慢得多。任何线索为什么这个慢?
这是整个代码
cat minimumcoinrecurse.py
import timeit
def change(amount):
money = ()
for coin in [25,10,5,1]:
num = amount/coin
money += (coin,) * num
amount -= coin * num
return money
#print change(63)
def get_min_coin_configuration(sum=None, coins=None, cache=None):
if cache == None: # this is quite crucial if its in the definition its presistent ...
cache = {}
if sum in cache:
return cache[sum]
elif sum in coins: # if sum in coins, nothing to do but return.
cache[sum] = [sum]
#print cache
return cache[sum]
elif min(coins) > sum: # if the largest coin is greater then the sum, there's nothing we can do.
cache[sum] = []
return cache[sum]
else: # check for each coin, keep track of the minimun configuration, then return it.
min_length = 0
min_configuration = []
for coin in coins:
results = get_min_coin_configuration(sum - coin, coins, cache)
if results != []:
if min_length == 0 or (1 + len(results)) < len(min_configuration):
#print "min config", min_configuration
min_configuration = [coin] + results
#print "min config", min_configuration
min_length = len(min_configuration)
cache[sum] = min_configuration
#print "second print", cache
return cache[sum]
def main():
print "recursive method"
print "time taken",
t=timeit.Timer('get_min_coin_configuration(63,[25,10,5,1])',"from __main__ import get_min_coin_configuration")
print min(t.repeat(3,100))
print get_min_coin_configuration(63,[25,10,5,1])
print '*'*45
print "non-recursive"
print "time taken",
t1=timeit.Timer('change(63)',"from __main__ import change")
print min(t1.repeat(3,100))
print change(63)
if __name__ == "__main__":
main()
输出:
recursive method
time taken 0.038094997406
[25, 25, 10, 1, 1, 1]
*********************************************
non-recursive
time taken 0.000286102294922
(25, 25, 10, 1, 1, 1)
答案 0 :(得分:1)
评估中未使用缓存。而且,它每次运行都会重建。与
比较cache = {}
def main():
print "recursive method"
print "time taken",
通过明确指定它来使用缓存:
t=timeit.Timer('get_min_coin_configuration(63, [25,10,5,1], cache)',
'from __main__ import get_min_coin_configuration, cache')
print min(t.repeat(3,100))
print get_min_coin_configuration(63,[25,10,5,1])
print '*'*45
print "non-recursive"
print "time taken",
t1=timeit.Timer('change(63)',"from __main__ import change")
print min(t1.repeat(3,100))
print change(63)
recursive method
time taken 8.26920739926e-05
[25, 25, 10, 1, 1, 1]
*********************************************
non-recursive
time taken 0.000361219093488
(25, 25, 10, 1, 1, 1)