Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
negative memory usage
#1
hi
in the below code, after running, the usage of memory is shown as a negative number! why?
#from:https://blog.faradars.org/reduce-memory-usage-and-make-your-python-code-faster-using-generators/
'''generators'''

'''
problem to be solved: in a large list( for example 100,000,000) calculate the cube of even numbers.
'''

import memory_profiler
import time

#ordianary method:
def check_even_list(numbers):
    even = []
    for num in numbers:
        if num % 2 == 0: 
            even.append(num*num)
            
    return even

#using generator
def check_even_gene(numbers):
    for num in numbers:
        if num % 2 == 0:
            yield num * num 
    

    
if __name__ == '__main__':
    n=100_000_000
    #n=100
    m1 = memory_profiler.memory_usage()
    t1 = time.time()
    cubes = check_even_list(range(n))  #ordinary method with using list
    t2 = time.time()
    m2 = memory_profiler.memory_usage()       # breakpoint set here in this line
    time_diff = t2 - t1
    mem_diff = m2[0] - m1[0]
    print(f"It took {time_diff} Secs and {mem_diff} Mb to execute this method with using list.")
    
    
    m3 = memory_profiler.memory_usage()
    t3 = time.time()
    cubes = check_even_gene(range(n))
    t4 = time.time()
    m4 = memory_profiler.memory_usage()
    time_diff = t4 - t3
    mem_diff = m4[0] - m3[0]
    print(f"It took {time_diff} Secs and {mem_diff} Mb to execute this method with using generator.")
   

my output in Thonny:
Output:
It took 27.66528820991516 Secs and 1913.3046875 Mb to execute this method with using list. It took 1.9990813732147217 Secs and -1912.48828125 Mb to execute this method with using generator.
if I change n to 100, the consumed time is zero as below, why?
Output:
It took 0.0 Secs and 0.00390625 Mb to execute this method with using list. It took 0.0 Secs and 0.0 Mb to execute this method with using generator.
thanks for any reply
Reply
#2
(Apr-27-2024, 07:41 AM)akbarza Wrote: the usage of memory is shown as a negative number! why?
I think it is negative because the code deletes the previously calculated list cubes. You could correct this by running
del cubes
m3 = ...
Another major problem with your code is that the call to check_even_gene() creates a generator but it does not actually calculate the data num * num, so it basically does nothing.

Also note that num * num is not the cube of the number num.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting an error while trying to process data, low memory when memory is not low? bkeith12 0 1,221 Dec-20-2024, 03:06 PM
Last Post: bkeith12
  cmath.rect accepts a negative modulus JMB 2 7,239 Jan-17-2024, 08:00 PM
Last Post: JMB
  How to do bar graph with positive and negative values different colors? Mark17 1 10,639 Jun-10-2022, 07:38 PM
Last Post: Mark17
  is there any tool to convert negative base to int? Skaperen 7 4,687 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 3,609 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Def code does not work for negative integers. doug2019 1 3,063 Oct-31-2019, 11:00 PM
Last Post: ichabod801
  offset can not be negative in File.seek()? jollydragon 6 10,546 Sep-28-2019, 03:08 AM
Last Post: jollydragon
  Positive to negative bernardoB 6 6,674 Mar-13-2019, 07:39 PM
Last Post: bernardoB
  How to get memory usage and execution time of each line in python SriRajesh 2 5,727 Mar-07-2019, 12:59 PM
Last Post: SriRajesh
  Python regex with negative set of characters multiline sonicblind 2 4,652 Jul-30-2018, 08:43 PM
Last Post: sonicblind

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020