我正在尝试使用装饰器(使用guppy.hpy)来计算内存计算,如下面的代码所示。 但是,当您第一次使用它时,它会给出正确的结果,但是当我第二次调用相同的函数时,内存分配更多,请有人帮忙。
#!/usr/bin/python
import itertools
from guppy import hpy
def Memory_check(f):
def wrap(*args,**kwargs):
hp = hpy()
start=hp.heap()
ret=f(*args,**kwargs)
end=hp.heap()
used=end-start
print("{} took {}".format(f.__name__,used[0]))
return ret
return wrap
@Memory_check
def func1(stra):
return [''.join(x) for x in itertools.permutations(stra)]
@Memory_check
def func2(stra):
return [''.join(x) for x in itertools.permutations(stra)]
ret=func1('0123')
ret1=func2('0123')
结果
**func1** took Partition of a set of 6 objects. **Total size = 1680 bytes.**
Index Count % Size % Cumulative % Kind (class / dict of class)
0 6 100 1680 100 1680 100 dict (no owner)
**func2** took Partition of a set of 93 objects. **Total size = 37560 bytes.**
Index Count % Size % Cumulative % Kind (class / dict of class)
0 93 100 37560 100 37560 100 dict (no owner)