Skip to main content

Mark potentially slow blocks for notifications when it actually turns out too slow, so you can optimize it.

Project description

optimize-later Build Status Coverage PyPI PyPI - Format PyPI - Django Version

Premature optimization is the root of all evil (or at least most of it) in programming.

-- Donald Knuth

Wouldn't it be nice to have something to tell you when optimization is really necessary?

Enter optimize-later.

Instead of trying to guess what code ought to be optimized, optimize-later times potentially slow blocks of code for you, and calls a user-specified function when it exceeds the specified time limit. This way, you only have to optimize code when speed becomes a problem, saving you from both the evils of premature optimization, and the evils of slow code.


from optimize_later import optimize_later, register_callback

### Basic usage.
with optimize_later('test_block', 0.2):
    # potentially slow block of code...

def my_report_function(report):
    # Short one line description.

    # Long description with breakdown based on blocks.

    # Details available in:
    #   - block name
    #   - report.limit: time limit
    #   - time consumed
    #   - report.blocks: breakdown by blocks
    #   - report.start, report.end: start and end time with an unspecified timer:
    #     useful for building a relative timeline with blocks.

### More advanced uses.
# Automatic block names from file and source line (slightly slow).
with optimize_later(0.2):
    # potentially slow block of code...

# Always warn. Good for exceptional cases that you suspect should not happen.
with optimize_later():
    # potentially slow block of code...

# Also available as a decorator.
@optimize_later('bad-function', 0.2)
def function_name():
    # potentially slow function...

# Will use module:function as block name, if you do not specify a name.
# There is no performance penalty this way, as the function name can be easily detected.
def function_name():
    # potentially slow function...

### Blocks.
with optimize_later() as o:
    with o.block('block 1'):
        # When the time limit of whole block is exceeded, your report will contain
        # a detailed breakdown by sub-blocks executed. This allows you to pinpoint
        # which exact block is the culprit.

    # optimize-later will automatically generate a block name for you from file and
    # line number, with a slightly performance penalty.
    with o.block() as b:
        # You can also nest blocks.
        with b.block():

### Callbacks deregistration and contexts.
from optimize_later import deregister_callback, optimize_context


with optimize_context():
    # Register a callback here.
# Callback is not available here.

def function():
    # This callback will be available for the duration of this function.

# Remove global callbacks for this block.
with optimize_context(renew=True):
# or...
def function():

# Shortcut registration syntax.
with optimize_context(my_report_function):

@optimize_context(my_report_function, renew=True)
def function():

A sample short report:

Block '' took 0.011565s (+0.011565s over limit)

A sample long report:

Block '' took 0.011565s (+0.011565s over limit), children:
  - Block '' took 0.006662s, children:
      - Block '' took 0.000002s
      - Block '' took 0.000002s
  - Block '' took 0.000001s


Install the module with:

$ pip install optimize-later

Or if you want the latest bleeding edge version:

$ pip install -e git://

That's it!


If you are using Django, you might want to configure optimize-later in instead of adding callbacks directly.

You have to add 'optimize_later' to INSTALLED_APPS.

Then, the list of callbacks as dot-separated import paths can be specified in 'OPTIMIZE_LATER_CALLBACKS' in For example:


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for optimize-later, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size optimize_later-0.3-py3-none-any.whl (10.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size optimize-later-0.3.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page