def print_num_stats(stats, units=None, formats=None, file=None):
    """
    Utility function to print results of `num_stats` function.

    >>> print_num_stats(num_stats(range(10)), units={'count':'iterations'}, formats={'sum':'%.5f'})
    count 10 iterations
    sum 45.00000
    mean 4.500
    sd 2.872
    min 0.000
    1% 0.090
    5% 0.450
    25% 2.250
    50% 4.500
    75% 6.750
    95% 8.550
    99% 8.910
    max 9.000
    >>> print_num_stats(num_stats(range(10)), formats={'sum':'', '1%':'', '5%':''})
    count 10
    mean 4.500
    sd 2.872
    min 0.000
    25% 2.250
    50% 4.500
    75% 6.750
    95% 8.550
    99% 8.910
    max 9.000
    """

    def get_from(d, key, default):
        if d is None:
            return default
        return d.get(key, default)

    for key, value in stats.items():
        fmt = "%.3f"
        if isinstance(value, int):
            fmt = "%d"
        fmt = get_from(formats, key, fmt)
        if fmt == '':
            continue

        unit = get_from(units, key, '')
        if unit:
            unit = ' ' + unit

        pstr = "%s %s%s" % (key, fmt % value, unit)
        if file is None:
            print(pstr)
        else:
            print(pstr, file=file)