0

I have this code, but I want to print the time that a specific part of it takes. The block of code that I want to know the time is between:

def TSP(lista2d):
    import copy
    origen = input("Ciudad de origen: ")
    resultado = [int(origen)]
    iteracion, indicador = int(origen) - 1, 0
    distancia, copia = [], copy.deepcopy(lista2d)
    for j in range(1, len(lista2d)):
        for x in range(len(lista2d)):
            lista2d[x][iteracion] = 999
        distancia.append(min(lista2d[iteracion]))
        for i in range(len(lista2d)):
            if min(lista2d[iteracion]) == lista2d[iteracion][i]:
                indicador = i
        lista2d[indicador][iteracion] = 999
        resultado.append(indicador + 1)
        iteracion = indicador
    resultado.append(int(origen))
    a = copia[resultado[-2] - 1][int(origen) - 1]
    distancia.append(a)
    print("El camino mas corto: " + str(resultado) + "\nCosto total: " + str(sum(distancia)))

TSP([[999,100,150,140,130,120,78,150,90,200,180,190,160,135,144,300,60,77,87,90],
     [100,999,200,180,190,160,135,144,90,150,140,130,120,78,300,160,88,99,87,95],
     [150,200,999,167,156,169,123,134,156,177,155,188,176,143,192,146,170,152,176,122],
     [140,180,167,999,190,198,213,321,252,123,234,111,112,114,167,189,203,205,234,300],
     [130,190,156,190,999,333,300,178,167,143,200,111,156,267,299,152,100,90,97,99],
     [120,160,169,198,333,999,480,389,412,500,253,222,333,378,287,273,266,255,199,201],
     [78,135,123,213,300,480,999,140,150,143,177,194,166,200,181,154,177,133,122,109],
     [150,144,134,321,178,389,140,999,149,129,129,136,156,177,141,186,175,153,133,122],
     [90,90,156,252,167,412,150,149,999,89,82,83,60,124,59,78,89,99,100,123],
     [200,150,177,123,143,500,143,129,89,999,99,200,254,233,211,197,183,154,167,169],
     [180,140,155,234,200,253,177,129,82,99,999,77,88,89,289,222,311,471,122,109],
     [190,130,188,111,111,222,194,136,83,200,77,999,91,90,93,106,132,100,98,35],
     [160,120,176,112,156,333,166,156,60,254,88,91,999,102,103,107,111,113,200,101],
     [135,78,143,114,267,378,200,177,124,233,89,90,102,999,77,79,201,166,173,102],
     [144,300,192,167,299,287,181,141,59,211,289,93,103,77,999,55,103,105,101,201],
     [300,160,146,189,152,273,154,186,78,197,222,106,107,79,55,999,76,78,84,92],
     [60,88,170,203,100,266,177,175,89,183,311,132,111,201,103,76,999,93,102,29],
     [77,99,152,205,90,255,133,153,99,154,471,100,113,166,105,78,93,999,88,65],
     [87,87,176,234,97,199,122,133,100,167,122,98,200,173,101,84,102,88,999,333],
     [90,95,122,300,99,201,109,122,123,169,109,35,101,102,201,92,29,65,333,999]])
1

1 Answer 1

1

You can use 'time.time()' for example

Start_time = time.time()
End_time = time.time()
Diff_time = End_time()-Start_time()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.