Python在使用包装器函数时增加了内存使用率?

2024-10-03 21:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我为一个程序编写了一个测试,并检查了内存使用情况。由于某些原因,在函数中包装相同的操作会导致内存使用量的多项式(或指数)增长。我使用memory_profiler来跟踪内存使用情况,python2.7.1。在

代码:

def convertIntToBitint(number): return (pow(2, number - 1))

测试代码和结果(清理了一点):

^{pr2}$

为什么convertIntToBitint占用更多内存?为什么内存使用量没有线性增长?在

编辑2rs2ts

有趣。不知道这是否是你的意思。在

Line #    Mem usage    Increment   Line Contents
23     9.074 MB     0.000 MB    def test_convertBigIntToBitint(self):
24    56.691 MB    47.617 MB        result3 = convertIntToBitint(121000000)
25    85.539 MB    28.848 MB        answer3 = pow(2, 120999999)
26    85.539 MB     0.000 MB        result2 = convertIntToBitint(5015280)
27    84.258 MB    -1.281 MB        answer2 = pow(2, 5015279)
28    83.773 MB    -0.484 MB        result1 = convertIntToBitint(21000)
29    81.211 MB    -2.562 MB        answer1 = pow(2, 20999)

Tags: 函数内存程序numberdefline情况原因