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
3
votes
1 answer

Python cProfile - decorated functions obscuring profile visualization

I have a base class with a @classmethod which acts as a decorator for a large number of methods in many descendant classes. class BaseClass(): @classmethod def some_decorator(cls, method): @wraps(method) def…
Eric Ihli
  • 1,722
  • 18
  • 30
3
votes
1 answer

Can't figure out how to invoke cProfile inside of a program

Sorry for the beginner question, but I can't figure out cProfile (I'm really new to Python) I can run it via my terminal with: python -m cProfile myscript.py But I need to run it on a webserver, so I'd like to put the command within the script it…
Parker
  • 8,539
  • 10
  • 69
  • 98
3
votes
3 answers

How can I use the cProfile module to time each funtion in python?

I have a program in Python that takes in several command line arguments and uses them in several functions. How can I use cProfile (within my code)to obtain the running time of each function? (I still want the program to run normally when it's…
Claira
  • 31
  • 1
  • 2
3
votes
2 answers

cprofile compile error on python

using cProfile of python, I cprofiled my code, but I keep getting this error related with compile() and null character which I can't quite understand. The error message is: [cProfileV]: cProfile output available at http://127.0.0.1:4000 …
3
votes
1 answer

Making use of pstats and cProfile in Python. How to make array work faster?

This is my first optimization of the code and I am excited about it. Read some articles but I still have some questions. 1) First of all, what does take so much time in my code below? I think it is array over here:…
Sergey
  • 67
  • 5
3
votes
2 answers

Where does uWSGI put its profiler file?

I'm using uWSGI with the "--profiler" flag switched on but I can't seem to find the output file (if there is one), and (like many things in uWSGI) this isn't properly documented.
itzhaki
  • 822
  • 2
  • 7
  • 19
3
votes
1 answer

Call Python cProfile from within a function. (Or other way to use cProfile with Django)

How do I call cProfile from within a function, using it to call and profile another function? I have a function start(), which is called from my webpage (using Django). In this function I place the cProfile call: cProfile.run('my_function()') This…
user984003
  • 28,050
  • 64
  • 189
  • 285
2
votes
0 answers

using cProfile to profile a code with decorators have strange results

So i'm using cProfile and snakeviz to profil my code. My code has decorator which seems to make the cProfile results not very correct : my code : import time import decorator @decorator.decorator def my_deco(f, *args, **kwargs): ret = f(*args,…
A.Vignon
  • 365
  • 1
  • 10
2
votes
1 answer

How to omit methods in cProfile

When I profile code which heavily uses the all/any builtins I find that the call graph as produced by profiling tools (like gprof2dot) can be more difficult to interpret, because there are many edges from different callers leading to the all/any…
Joe
  • 368
  • 3
  • 13
2
votes
1 answer

PySimpleGUI / Tk: very slow startup on Mac

I was spending a lot of time trying to understand why loading time of PySimpleGUI with Tk on Mac OS X takes so much time. To be more specific: window.refresh() takes 9 seconds for my (quite simple) full UI. I reduced it for the investigation, so…
ishahak
  • 6,585
  • 5
  • 38
  • 56
2
votes
1 answer

Same Python code appears to have different performance characteristics

Description In my code to create N random strings of length k, a generator expression to create a random k-character string gets called either N+1 or (N+1)*k (aka N*k+N) times. This appears to be happening with the exact same code and I can switch…
rajatsbugs
  • 45
  • 5
2
votes
1 answer

Why is pstats.print_callers() ignores the restriction argument?

I'm trying to profile my python script using cProfile and displaying the results with pstats. In particular, I'm trying to use the pstats function p.sort_stats('time').print_callers(20) to print only the top 20 functions by time, as decribed in the…
Elegant Code
  • 678
  • 6
  • 18
2
votes
2 answers

Using cProfile on application with entry point

I know cProfile can be used like this python -m cProfile script.py But suppose I have a packaged application (pip-installable) with an entry_point defined in setup.py, such that I can call the application from the terminal, without explicitly using…
foxpal
  • 586
  • 4
  • 14
2
votes
1 answer

Python script long execuation function

Hello i got a problem that a function in my script takes long to execute. So i am trying to optimize the code and i am currently using cProfile and Pstats for that. When i execute a function it takes around 0.7 seconds to 1+ seconds to finish. The…
Patrick
  • 23
  • 3
2
votes
1 answer

Fastest screenshot library python/improve performance of mss package

python 3.6, Windows 10: I am trying to take one (partial) screenshot every 1-5 milliseconds to then run some custom OCR on it to extract some data. My code for taking the screenshots using the mss package takes between 16 and 47ms depending upon…
user2136502
  • 69
  • 1
  • 3