Coder Social home page Coder Social logo

botify-labs / statprof Goto Github PK

View Code? Open in Web Editor NEW

This project forked from smarkets/statprof

0.0 17.0 1.0 243 KB

Smarkets fork of a port of Andy Wingo's statprof.py to newer python by Alex Fraser

License: GNU Lesser General Public License v2.1

Python 100.00%

statprof's Introduction

statprof - statistical profiling for Python

image

image

This package provides a simple statistical profiler for Python.

Python's default profiler has been lsprof for several years. This is an instrumenting profiler, which means that it saves data on every action of interest. In the case of lsprof, it runs at function entry and exit. This has problems: it can be expensive due to frequent sampling, and it is blind to hot spots within a function.

In contrast, statprof samples the call stack periodically (by default, 1000 times per second), and it correctly tracks line numbers inside a function. This means that if you have a 50-line function that contains two hot loops, statprof is likely to report them both accurately.

Note

This package does not yet work on Windows! See the implementation and portability notes below for details.

How to get it

Use pip!

pip install statprof-smarkets

Warning: it uses statprof as Python module name so this will conflict with original statprof installation if present.

GitHub project page: https://github.com/smarkets/statprof

PyPI page: https://pypi.python.org/pypi/statprof-smarkets

Basic usage

It's easy to get started with statprof:

import statprof

statprof.start()
try:
    my_questionable_function()
finally:
    statprof.stop()
    statprof.display()

Or with a contextmanager:

import statprof

with statprof.profile():
    my_questionable_function()

Or from command line:

$ python -m statprof script.py
# or
$ python -m statprof -m script

For more comprehensive help, run pydoc statprof.

Portability

Because statprof uses the Unix itimer signal facility, it does not currently work on Windows. (Patches to improve portability would be most welcome.)

Implementation notes

The statprof profiler works by setting the Unix profiling signal ITIMER_PROF to go off after the interval you define in the call to reset(). When the signal fires, a sampling routine is run which looks at the current procedure that's executing, and then crawls up the stack, and for each frame encountered, increments that frame's code object's sample count. Note that if a procedure is encountered multiple times on a given stack, it is only counted once. After the sampling is complete, the profiler resets profiling timer to fire again after the appropriate interval.

Meanwhile, the profiler keeps track, via os.times(), how much CPU time (system and user -- which is also what ITIMER_PROF tracks), has elapsed while code has been executing within a start()/stop() block.

The profiler also tries (as much as possible) to avoid counting or timing its own code.

Changelog

0.2.0 (not released yet)

  • forked
  • refactored
  • added configurable display format (displays full paths by default now)
  • ability to run whole scripts under statprof from command line (thanks to Vincent Driessen and Antony Lee

History

This package was originally written and released by Andy Wingo. It was ported to modern Python by Alex Frazer, and posted to GitHub by Jeff Muizelaar. Maintained by Bryan O'Sullivan, was forked by Smarkets due to package not being maintaned anymore.

Reporting bugs, contributing patches

Please report bugs using the GitHub issue tracker.

If you'd like to contribute patches, please do - the source is on GitHub, so please just issue a pull request.

$ git clone git://github.com/smarkets/statprof

statprof's People

Contributors

anntzer avatar bos avatar cyberdelia avatar durhamg avatar florentx avatar jedbrown avatar jrmuizel avatar jstasiak avatar nvie avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.