w3resource

Python: Determine profiling of Python programs

Python Basic: Exercise-51 with Solution

Write a Python program to determine profiling of Python programs.

Note: A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.

Sample Solution:-

Python Code:

import cProfile
def sum():
    print(1+2)
cProfile.run('sum()')

Sample Output:

3                                                                                                             
         5 function calls in 0.000 seconds                                                                    
                                                                                                              
   Ordered by: standard name                                                                                  
                                                                                                              
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)                                       
        1    0.000    0.000    0.000    0.000 7aa14930-2430-11e7-807b-bd9de91b1602.py:2(sum)                  
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)                                            
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}                                 
        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}                                
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}  
	

Flowchart:

Flowchart: Determine profiling of Python programs.

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print without newline or space?
Next: Write a Python program to print to stderr.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"
str.strip()

print(str)

Output:

+++Welcome python+++