Catch current time in Python

Asked

Viewed 90 times

-1

I want to calculate the execution time of a function in nanoseconds, thus:

tempoIniAlg = time_ns()
vetorOrdenado = quickSort(vetor)  # Vetor ordenado
tempoAlg = time_ns()  # Tempo de resolução do algortimo

The problem is that with a relatively high frequency, the fields tempoIniAlg and tempoAlg have exactly the same value, resulting in 0 the difference value between both, which is impossible for N reasons. I tried to do it this way and it didn’t solve anything:

tempoIniAlg = time_ns()
vetorOrdenado = quickSort(vetor)  # Vetor ordenado
tempoAlg = time_ns() - tempoIniAlg  # Tempo de resolução do algortimo

I have no idea what it might be.

  • 1

    If you want to measure runtime, you can use the module timeit: https://docs.python.org/3/library/timeit.html

1 answer

1

Try to use the library time python

import time #importa a biblioteca 
start_time = time.time()    # pega o tempo de inicio 

i = 0 # variavel de exemplo 
while True: # loop de exemplo
    i = i + 1 # soma 
    print(i) # exibe qual o loop que está
    if i == 2: # define um máximo de 125 loops
        break # para o loop 

print("--- %s seconds ---" % (time.time() - start_time)) # faz a conta de quanto demorou para executar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.