def timed(prefix=None):
    """
    Decorator to time execution of function.
    Metric name is function name (as given under f.__name__).
    Optionally provide a prefix (without the '.').

    >>> @timed()
    ... def f():
    ...     print('ok')
    ...
    >>> f()
    ok
    >>> @timed(prefix='mymetrics')
    ... def g():
    ...     print('ok')
    ...
    >>> g()
    ok
    """
    def decorator(func):
        """wrap func so it is timed"""
        metricname = func.__name__

        if prefix:
            metricname = prefix + '.' + metricname

        def wrapped(*args, **kwargs):
            """wrapped function"""
            return timeit(metricname, func, *args, **kwargs)
        return wrapped

    return decorator