import functools
import gc


def disable_gc(f):
    """
    Decorator to disable garbage collection during the execution of the
    decorated function.

    This can speed-up code that creates a lot of objects in a short amount of
    time.
    """
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        gc.disable()
        try:
            return f(*args, **kwargs)
        # re-enable the garbage collector in all cases
        finally:
            gc.enable()

    return wrapper