Questions tagged [cprofile]

cProfile is a built-in Python module that describes the run time performance of a program, providing a variety of statistics.

cProfile is a built-in module suitable for profiling Python programs. It's designed as a faster drop-in replacement for the profile implementation. cProfile is implemented as C-Extension which provides less overhead than the pure Python implementation of profile, but it may not be available in all Python implementations.

It can be used in a Python file:

import cProfile
cProfile.run('insert_your_code_to_profile_here')

But also from the command-line, to profile a script:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)

Sources:

230 questions
7
votes
0 answers

Can we find the location of the built-in method calls in python profiling?

When I profile my python code using cProfile, there are lots of {built-in method...} taking most of the time. They may be third party APIs or my C/Fortran-extensions. Question is that can we easily find where these methods were called in the python…
Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117
6
votes
1 answer

profiling a python module from the command line

I have a python project where I execute the app as a module using the -m flag. So something like: python -m apps.validate -i input.mp4 Now, I want to profile it using the command line. So the inline examples suggest invoking cProfile itself a…
Luca
  • 10,458
  • 24
  • 107
  • 234
6
votes
1 answer

How do we profile memory, CPU load for flask API's on runtime?

I'm using python 3 for my flask app which is running in a port 5000. It has one sample API. I need to profile memory, CPU usage while hitting this API from REST or Browser. Please help me out for better solution. import logging from flask import…
6
votes
1 answer

How to timeout code being profiled with cProfiler without modifying user code?

Often during my work I write code to read lines from a file and I process those lines one at a time. Sometimes the line processing is complicated and the file is long, for example, today it takes roughly a minute for processing 200 lines and the…
Pushpendre
  • 795
  • 7
  • 19
6
votes
1 answer

Python Profiling In Windows, How do you ignore Builtin Functions

I have not been capable of finding this anywhere online. I was looking to find out using a profiler how to better optimize my code, and when sorting by which functions use up the most time cumulatively, things like str(), print, and other similar…
Tim McJilton
  • 6,884
  • 6
  • 25
  • 27
6
votes
1 answer

cProfile taking a very long time

I am starting to use cProfile to profile my python script. I have noticed something very weird. When I use time to measure the running time of my script it takes 4.3 seconds. When I use python -m cProfile script.py it takes 7.3 seconds. When running…
user844541
  • 2,868
  • 5
  • 32
  • 60
6
votes
0 answers

No Output From cProfile

I'm trying to profile an application written using PySide and OpenCV and am getting strange behaviour with the profiler. I run my code using the following line: python -m cProfile -o output.file repo/src/application_window.py It tends to work for…
The Bearded Templar
  • 671
  • 1
  • 7
  • 16
6
votes
2 answers

python - cProfile not running

I was trying to run a performance test of my code using cProfile, but sadly no matter how I tried cProfile refused to function properly. Here's what I did: import cProfile cProfile.run('addNum()') # addNum() is a very simple function that adds a…
turtlesoup
  • 3,188
  • 10
  • 36
  • 50
5
votes
1 answer

Flask request.form.get too slow?

I am using Flask for my Web Api service. Finding that my services sometimes (1/100 requests) respond really slow (seconds), I started debugging, which showed me that sometimes the service hangs on reading the request field. @app.route('/scan',…
Dmitry Zuev
  • 142
  • 7
5
votes
1 answer

What is the meaning of function in the output of cProfile analyzed using KchacheGrind?

I want to analyze the performance of the python code, I have used cProfile module for that and generated the .cprof file as mentioned in the python documentation. I am using pyprof2calltree python module to open the .cprof file into KCacheGrind. . I…
Bhargav Upadhyay
  • 545
  • 5
  • 12
5
votes
1 answer

cProfile command line how to reduce output

I'm trying to run cProfile on my python script and all I care about is the total time it took to run. Is there a way to modify python -m cProfile myscript.py so the output is just the total time?
Adam Kall
  • 85
  • 1
  • 6
5
votes
2 answers

cProfile with imports

I'm currently in the process of learn how to use cProfile and I have a few doubts. I'm currently trying to profile the following script: import time def fast(): print("Fast!") def slow(): time.sleep(3) print("Slow!") def medium(): …
dudas
  • 403
  • 6
  • 23
5
votes
2 answers

Serious overhead in Python cProfile?

Hi expert Pythonists out there, I am starting to use cProfile so as to have a more detailed timing information on my program. However, it's quite disturbing to me that there's a significant overhead. Any idea why cProfile reported 7 seconds while…
user378289
  • 179
  • 2
  • 5
5
votes
0 answers

Ignoring function calls from cProfile log

I know that it is possible to get the frequency of function calls for a particular module using the pstats.print_stats('python_script.py:'), however I would like to print a report of the frequencies of all invoked functions in a profiled program…
erik
  • 3,810
  • 6
  • 32
  • 63
5
votes
1 answer

Error when attempting to write cProfile information to file

I am attempting to load a cProfile profile, do some sorting and finessing, then output the results to a file. Based on the documentation, I thought I could simply pass a file object and the print_stats function would redirect to that stream. Here…
erik
  • 3,810
  • 6
  • 32
  • 63