Tools

TimeIt

devhelpers.timeit.timeit(arg)[source]

Use the decorator to time functions in place.

One hazard: When you use the decorator with a function/method that does change something outside it’s namespace or a method changes anything inside the internal dict, and you let it repeat stuff, might result in an unexpected behavior.

Notes

It is safe:

  • when the function/method does not e.g. count, append something outside its own namespace, when using the decorator with repeating enabled like @timeit(100). Or in other words: if the internal state does not change, when you call it $ n+1 $ times.

  • if you don’t repeat anything: @timeit.

Examples

@timeit(100)
def foo(a, b):
    pass

@timeit
def bar(c, d):
    pass
Parameters

arg (Union[Callable[…, ~ReturnType], int]) – the callable or number of runs

Return type

Callable[…, ~ReturnType]

IsAtomic

exception devhelpers.isatomic.NotAtomicError(result_1, result_n, iteration)[source]

Bases: Exception

devhelpers.isatomic.isatomic(arg)[source]

Use the decorator to check if a function is atomic.

The decorator runs a function over and over again and compares the output. When the output changes, it raises a NotAtomicError.

Examples

@isatomic(100)
def foo(a, b):
    pass

@isatomic
def bar(c, d):
    pass
Parameters

arg (Union[Callable[…, ~ReturnType], int]) – the callable or number of runs

Return type

Callable[…, ~ReturnType]

NoGC

devhelpers.nogc.nogc(func)[source]

Use the decorator to disable the garbage collector for a function.

The garbage collector runs frequently to remove unreachable objects from memory. While running the @timeit decorator, to time functions it might be beneficial to disable the garbage collector.

Notes

By disabling the garbage collector, dangling object remain in memory until the garbage collector is re-enabled and the garbage is freed.

If you are sure, you don’t have a “leaking” function, this might not be a problem. But if the program is “leaky”, and you use e.g. the timeit decorator, each cycle new objects are created but never freed.

Make sure you understand the reference counter and the garbage collector to not use this decorator for the wrong reasons.

Examples

@nogc
@timeit(100)
def foo(a, b):
    pass

@nogc
@timeit
def bar(c, d):
    pass

@nogc
def baz():
    assert not gc.isenabled()
Return type

Callable[…, ~ReturnType]