[Python] What is the difference between time.perf_counter() and time.process_time()?

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Jupyter notebook. I'm trying to measure how long will it take to count the Avogadro's number with Python.

    I found that time.perf_counter() and time.process_time() modules will be useful for this kind of work. So I tried both of them, but the result was totally different. What makes this kind of difference?


    Here is my code.

    Using time.perf_counter()


    import time

    a = 10 ** 5

    def AvogadroCounting():
    i = 0
    while i <= a:
    i += 1

    AvogadroCounting()

    t_fract = time.perf_counter() #time to count fraction of avogadro's number in Seconds


    print(t_fract, 'secs')


    And my notebook gives 693920.393636181 secs.

    Using time.process_time()


    import time

    a = 10 ** 5

    def AvogadroCounting():
    i = 0
    while i <= a:
    i += 1

    AvogadroCounting()

    t_fract = time.process_time() #time to count fraction of avogadro's number in Seconds


    print(t_fract, 'secs')


    and this gives 2048.768273 secs.

    Continue reading...

Compartilhe esta Página