python - 内存分析器在所有步骤中提供恒定的内存

标签 python search memory-profiling

我想获取函数中每一步的内存变化。 我已经编写了插值搜索的代码,甚至给出了大至 10000 个的输入。列表中的元素,但内存中仍然没有变化。

代码是:

import time
from memory_profiler import profile

@profile()
def interpolation_search(numbers, value):
low = 0
high = len(numbers) - 1
mid = 0

while numbers[low] <= value and numbers[high] >= value:
    mid = low + ((value - numbers[low]) * (high - low)) / (numbers[high] -     numbers[low])

    if numbers[mid] < value:
        low = mid + 1

    elif numbers[mid] > value:
        high = mid - 1
    else:
        return mid

if numbers[low] == value:
    return low
else:
    return -1

if __name__ == "__main__":
# Pre-sorted numbers
numbers = [-100, -6, 0, 1, 5, 14, 15, 26,28,29,30,31,35,37,39,40,41,42]
num=[]
for i in range(100000):
    num.append(i)

value = 15

# Print numbers to search
print 'Numbers:'
print ' '.join([str(i) for i in numbers])

# Find the index of 'value'
start_time1 = time.time()
index = interpolation_search(numbers, value)

# Print the index where 'value' is located
print '\nNumber %d is at index %d' % (value, index)
print("--- Run Time %s seconds---" % (time.time() - start_time1))

我得到的输出是:

      Numbers:
     -100 -6 0 1 5 14 15 26 28 29 30 31 35 37 39 40 41 42
     Filename: C:/Users/Admin/PycharmProjects/timenspace/Interpolation.py

 Line #    Mem usage    Increment   Line Contents
 ================================================
 4     21.5 MiB      0.0 MiB   @profile()
 5                             def interpolation_search(numbers, value):
 6     21.5 MiB      0.0 MiB       low = 0
 7     21.5 MiB      0.0 MiB       high = len(numbers) - 1
 8     21.5 MiB      0.0 MiB       mid = 0
 9                             
10     21.5 MiB      0.0 MiB       while numbers[low] <= value and numbers[high] >= value:
11     21.5 MiB      0.0 MiB           mid = low + ((value - numbers[low]) * (high - low)) / (numbers[high] - numbers[low])
12                             
13     21.5 MiB      0.0 MiB           if numbers[mid] < value:
14                                         low = mid + 1
15                             
16     21.5 MiB      0.0 MiB           elif numbers[mid] > value:
17     21.5 MiB      0.0 MiB               high = mid - 1
18                                     else:
19     21.5 MiB      0.0 MiB               return mid
20                             
21                                 if numbers[low] == value:
22                                     return low
23                                 else:
24                                     return -1



 Number 15 is at index 6
   --- Run Time 0.0429999828339 seconds---

如您所见,我的内存在所有步骤中都保持在 21.5 Mib。

请帮我解决这个问题。谢谢

最佳答案

为什么你预计它会增加?我没有看到任何内存分配,即数组编号的大小没有增加

关于python - 内存分析器在所有步骤中提供恒定的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125243/

相关文章:

python - 从导航台数据存储聚合数据的最佳方式?

regex - 在 VS Code 中搜索多个术语

python - 什么使用我的 python 进程的内存? (RSS 与 VMS)

php - WordPress搜索-错误324(净::ERR_EMPTY_RESPONSE)

python - 解释 python memory_profiler 的输出

python-2.7 - memory_profiler 图中红色虚线的含义是什么

python - 属性错误 : 'ManyToManyDescriptor' object has no attribute 'all' - django

python - 使用 python 打印正整数 n 的 collat​​z 序列,每行一个值在 1 处停止

Python 相当于 Ruby 的 each_slice(count)

search - Perl 6中多于一根针的索引