python – formatting time delta

Very often, I measure myself the time it takes to part of my code. I normally do this by recording the current time with the help of time time () then subtract the time after the code is completed.

This normally gives me a float that I can then format and print for debugging purposes. This is the function I have for formatting the time:

def time_format (delta: float) -> str:
exit = []
    decimal, integer = math.modf (delta)

if whole:
minutes, seconds = divmod (int (integer), 60)

if minutes:
output.append ("% sm"% minutes)

if seconds:
output.append ("% ss"% seconds)

decimal, integer = math.modf (decimal * 1000)
if whole:
output.append ("% sms"% int (integer))

decimal, integer = math.modf (decimal * 1000)
if whole:
output.append ("% sμs"% int (integer))

decimal, integer = math.modf (decimal * 1000)
if whole:
output.append ("% sns"% int (integer))

returns "," .join (exit)

It was also said that the time module was the most accurate in time measurement, which is why I use it, instead of something like datetime that contains nice formatting tools integrated.

How can I improve my time formatting feature? Are there any built-in formatting tools I do not know? (The stack overflow search only leads me to questions about datetime.timedelta)