You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hi
in below code, i set breakpoint in line 49.
in ordinary running , the code is ok, but in debugging the code stoped and not response at all.
the code:
#from:https://blog.faradars.org/reduce-memory-usage-and-make-your-python-code-faster-using-generators/
'''generators'''
def generate_numbers():
n = 0
while n < 3:
yield n
n += 1
numbers = generate_numbers()
print(type(numbers)) #<class 'generator'>
next_number = generate_numbers()
next(next_number) #0
next(next_number) #1
next(next_number) #2
#next(next_number) # this line causes StopIteration error, because generator is exhausted.
'''
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__':
m1 = memory_profiler.memory_usage()
t1 = time.time()
cubes = check_even_list(range(100000000)) #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(100000000))
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.")
without dubugging and in normaly run , the code is runed in 22 second, but as i said in dubbuging( with breakpoint in line 49)the code goes in halt state and hangs.
The text was updated successfully, but these errors were encountered:
hi
in below code, i set breakpoint in line 49.
in ordinary running , the code is ok, but in debugging the code stoped and not response at all.
the code:
without dubugging and in normaly run , the code is runed in 22 second, but as i said in dubbuging( with breakpoint in line 49)the code goes in halt state and hangs.
The text was updated successfully, but these errors were encountered: