from time import perf_counter


def time_this(f):
    def wrapper(*arg, **kwargs):
        ts = perf_counter()
        res = f(*arg, **kwargs)
        te = perf_counter()
        if ts != te:
            ms = (te - ts) * 1000
            print("{:<32} {:>8.3f}ms {:>8.2f}fps".format(f.__qualname__, ms, 1000 / ms), flush=True)
        else:
            print("{:<32} {:>8.3f}ms {:>8.2f}fps".format(f.__qualname__, 0, "inf"), flush=True)
        return res
    return wrapper


last = {}


def clock_this(f):
    last[f] = perf_counter()

    def wrapper(*arg, **kwargs):
        ts = perf_counter()
        res = f(*arg, **kwargs)
        msc = (ts - last[f]) * 1000
        print("{:<32} {:>8.3f}ms".format(f.__qualname__, msc), flush=True)
        last[f] = ts
        return res
    return wrapper


def log_this(f):
    def wrapper(*arg, **kwargs):
        print("{:<32}".format(f.__qualname__), "begin", flush=True)
        res = f(*arg, **kwargs)
        print("{:<32}".format(f.__qualname__), "end", flush=True)
        return res
    return wrapper
