3

I am trying to measure the memory difference between a list created using a list comprehension to that created using a generator expression.

from memory_profiler import profile


@profile
def squares_using_generators():
    for square in (x ** 2 for x in range(200000)):
        if (square % 100000000 == 0):
            print(square)


@profile
def squares_using_list_comprehensions():
    from sys import getsizeof
    l = [x ** 2 for x in range(200000)]
    print('list size {}'.format(getsizeof(l)))
    for square in l:
        if(square % 100000000 == 0):
            print(square)


if __name__=='__main__':
    squares_using_generators()
    squares_using_list_comprehensions()

Memory profile output of the above program is as follows

For squares_using_generators()

Line #    Mem usage    Increment   Line Contents
================================================
     4     37.9 MiB      0.0 MiB   @profile
     5                             def squares_using_generators():
     6     37.9 MiB      0.0 MiB       for square in (x ** 2 for x in range(200000)):
     7     37.9 MiB      0.0 MiB           if (square % 100000000 == 0):
     8     37.9 MiB      0.0 MiB               print(square)

For squares_using_list_comprehensions

Line #    Mem usage    Increment   Line Contents
================================================
    11     37.9 MiB      0.0 MiB   @profile
    12                             def squares_using_list_comprehensions():
    13     37.9 MiB      0.0 MiB       from sys import getsizeof
    14     45.1 MiB      7.2 MiB       l = [x ** 2 for x in range(200000)]
    15     45.1 MiB      0.0 MiB       print('list size {}'.format(getsizeof(l)))
    16     45.2 MiB      0.1 MiB       for square in l:
    17     45.2 MiB      0.0 MiB           if(square % 100000000 == 0):
    18     45.2 MiB      0.0 MiB               print(square)

My program also prints the list size in case of squares_using_list_comprehensions

list size 1671792

My question is difference in Memory in the two cases in 7.2MiB. Why is it not 1671792 Bytes

liv2hak
  • 14,472
  • 53
  • 157
  • 270

0 Answers0