Search code examples
pythonsearchmemory-profiling

Memory Profiler giving constant memory in all steps


I want to get the change in memory for every step in my function. I have written the code for interpolation search and even given a input as large as 10000 no. of elements in a list, but still no change in memory.

The code is:

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))

The output that I am getting is:

      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---

As you can see my memory remains constant at 21.5 Mib in all steps.

Please help me with this.Thank You


Solution

  • Why do you expect it to increase? I don't see any memory allocations, i.e., the array numbers does not grow in size