[Python] Why does the output is 4 seconds, but real time of work is less?

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple decorator to measure time of function in seconds

    from functools import wraps
    from time import time

    def time_it(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
    start_time = time()
    res = func(*args, **kwargs)
    print("Time: ", time() - start_time)
    return res
    return wrapper


    @time_it
    def cycle():
    res = 0
    for i in range(1000):
    res += i
    return res



    cycle()


    The code outputs "Time: 4.4" I see that function works faster than 4 seconds

    Also checked with help of time utility:

    time python 0_decorators/decorator.py
    Time: 4.38690185546875e-05
    python 0_decorators/decorator.py 0.02s user 0.02s system 65% cpu 0.059 total


    Why does the output is 4 seconds, but real time of work is less? (I am using python3.11 and MacOS 12)

    Continue reading...

Compartilhe esta Página