Python Forum
runctx method of cProfile module.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
runctx method of cProfile module.
#1
Define two functions f1 and f2 such that,
f1 returns a list of squares of first 200000 natural numbers using list comprehension.
f2 returns a generator that computes squares of first 200000 natural numbers.
Perform profiling of both the functions and which portions of a function consume more time for computing.
Hint : Make use of runctx method of cProfile module.
Reply
#2
Welcome to the forum. We are glad to help, but we are not here to do your homework for you.
Please, post your code in python tags. Full traceback, if any - in error tags. Ask specific questions. Just posting your assignment will not do.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-21-2019, 10:53 AM)rahulnatarajan98 Wrote: Define two functions f1 and f2 such that, f1 returns a list of squares of first 200000 natural numbers using list comprehension. f2 returns a generator that computes squares of first 200000 natural numbers. Perform profiling of both the functions and which portions of a function consume more time for computing. Hint : Make use of runctx method of cProfile module.

Below is the code but it is not working properly (incorrect result)
Anyone can help me

import pstats, cProfile

def f1(n1):
    f= [i**2 for i in range(1,n1)]
    return f

def f2(n2):
    g = (x**2 for x in range(1,n2))
    return g

cProfile.runctx("f1(n1)", globals(), {'n1':200001}, "Profile1.prof")
cProfile.runctx("f2(n2)", globals(), {'n2':200001}, "Profile2.prof")

s1 = pstats.Stats("Profile1.prof")
s1.strip_dirs().sort_stats("time").print_stats()    

s2 = pstats.Stats("Profile2.prof")
s2.strip_dirs().sort_stats("time").print_stats()
Reply
#4
why do you think the result is incorrect?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
import cProfile
def f1():
 f = [i**2 for i in range(1,200001)]
 return f
def f2():
 g = (j**2 for j in range(1,200001))
 yield g
cProfile.runctx('f1()',globals(),locals())
cProfile.runctx('f2()',globals(),locals())

'''here's ur solution'''
Reply
#6
@FazimKarim, OP assignment requires f2 to return generator, not yield generator
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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