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
  is there any tool to convert negative base to int? Skaperen 7 2,449 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,258 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Def code does not work for negative integers. doug2019 1 1,953 Oct-31-2019, 11:00 PM
Last Post: ichabod801
  offset can not be negative in File.seek()? jollydragon 6 7,069 Sep-28-2019, 03:08 AM
Last Post: jollydragon
  Positive to negative bernardoB 6 4,436 Mar-13-2019, 07:39 PM
Last Post: bernardoB
  How to get memory usage and execution time of each line in python SriRajesh 2 3,168 Mar-07-2019, 12:59 PM
Last Post: SriRajesh
  negative to positive slices Skaperen 3 3,675 Jan-29-2018, 05:47 AM
Last Post: Skaperen
  Pyglet Media: unjustifiable memory usage hbknjr 4 4,634 Aug-20-2017, 02:08 AM
Last Post: hbknjr
  Negative lookahead not working, help please MitchBuchanon 8 6,009 Mar-01-2017, 02:41 PM
Last Post: Ofnuts
  Negative numbers and fractional powers Flexico 1 4,918 Dec-08-2016, 04:12 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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