0

Estoy aprendiendo a programar en Python, con el libro "Data Structures and Algorithms with Python", de Kent D.Lee, y me ha surgido un problema con el cálculo del tiempo que le lleva al ordenador ejecutar una función (que ordena una lista de números) en función de la cantidad de números que tenga la lista.

Para ello uso un programa,que cuenta el tiempo que tarda en ejecutar las función selSort() con una lista cada vez más grande. En la lista x guarda el valor de la cantidad de numero que tiene la lista y en la lista y el tiempo que ha tardado. Una vez llenadas esta listas, genera un archivo XML para luego graficar los resultados.

El problema es que cuando ejecuto el programa siempre obtengo un valor de tiempos, en la lista y, igual a cero, de la siguiente forma.

<Plot title="Average List Element Access Time">
<Axes>
<XAxis min="10" max="30">List Size</XAxis>
<YAxis min="0.0" max="453.0">Microseconds</YAxis>
</Axes>
<Sequence title="Average Access Time vs List Size" color="red">
<DataPoint x="10" y="0.0"/>
<DataPoint x="11" y="0.0"/>
<DataPoint x="12" y="0.0"/>
<DataPoint x="13" y="0.0"/>
<DataPoint x="14" y="211.0"/>
<DataPoint x="15" y="0.0"/>
<DataPoint x="16" y="0.0"/>
<DataPoint x="17" y="0.0"/>
<DataPoint x="18" y="0.0"/>
<DataPoint x="19" y="0.0"/>
<DataPoint x="20" y="0.0"/>
<DataPoint x="21" y="0.0"/>
<DataPoint x="22" y="0.0"/>
<DataPoint x="23" y="0.0"/>
<DataPoint x="24" y="0.0"/>
<DataPoint x="25" y="0.0"/>
<DataPoint x="26" y="0.0"/>
<DataPoint x="27" y="0.0"/>
<DataPoint x="28" y="0.0"/>
<DataPoint x="29" y="0.0"/>
<DataPoint x="30" y="453.0"/>
</Sequence>
</Plot>

Excepto algún valor puntual, en este caso y="211.0" e y="453.0".

He intentado cambiar las variables xmin y xmax, y las unidades del tiempo en el que represento los elementos de la lista y pero sigo sin tener buenos resultados.

A continuación dejo el código empleado para generar el archivo XML:

import datetime 
import random
import time
from random import randint
from random import randrange

def main():
    def select(seq, start):
        minIndex = start
        
        for j in range(start+1, len(seq)):
            if seq[minIndex] > seq[j]:
                minIndex = j
            
        return minIndex
    
    def selSort(seq):
        for i in range(len(seq)-1):
            minIndex = select(seq, i)
            tmp = seq[i]
            seq[i] = seq[minIndex]
            seq[minIndex] = tmp
        
    #genero una lista de n números aleatorios
    def listaAleatorios(n):
        lista = [0]  * n
        for i in range(n):
            lista[i] = random.randint(0, 1000)
        return lista
    
    # Write an XML file with the results
    file = open("Computer_Complexity_of_codes_4.5.1+4.5.2 .xml","w")
    
    file.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
    
    file.write('<Plot title="Average List Element Access Time">\n')
    
    # Test lists of size 10 to 200
    xmin = 10
    xmax = 30
    
    # Record the number sizes in xList and the average access time within
    # a list that size in yList for 1000 retrievals. 
    xList = []
    yList = []
    
    
    
    
    
    for x in range(xmin, xmax+1, 1):

        xList.append(x)
        
            
        # let any garbage collection/memory allocation complete or at least
        # settle down
        time.sleep(1)
        
        #genero una lista de números aleatorios de x elementos de la coordenada x
        aleatorios=listaAleatorios(x)
        #genero una lista de números aleatorios de x elementos de la coordenada y
      
        # Time before the 1000 test retrievals
        starttime = datetime.datetime.now()
        
        selSort(aleatorios)
          
           
        # Time after the 1000 test retrievals  
        endtime = datetime.datetime.now()
        
        # The difference in time between start and end.
        deltaT = endtime - starttime
        
        # Divide by 1000 for the average access time
        # But also multiply by 1000000 for microseconds.
        accessTime = deltaT.total_seconds() 
        accessTime = accessTime * 1000
        
        yList.append(accessTime)
     
    file.write('  <Axes>\n')
    file.write('    <XAxis min="'+str(xmin)+'" max="'+str(xmax)+'">List Size</XAxis>\n')
    file.write('    <YAxis min="'+str(min(yList))+'" max="'+str(max(yList))+'">Microseconds</YAxis>\n')
    file.write('  </Axes>\n')
    
    file.write('  <Sequence title="Average Access Time vs List Size" color="red">\n')   
    
    for i in range(len(xList)):   
        file.write('    <DataPoint x="'+str(xList[i])+'" y="'+str(yList[i])+'"/>\n')    
        
    file.write('  </Sequence>\n') 
    file.write('</Plot>\n')
    file.close()      
    
if __name__ == "__main__":
    main()
        

Y este segundo código, es el que grafica ese archivo XML, por si lo necesitais para entender la solución:

import turtle
import tkinter
import tkinter.colorchooser
import tkinter.filedialog
import xml.dom.minidom
import math
import sys

class PlotApplication(tkinter.Frame):
    def __init__(self, master=None, datafile=None):
        super().__init__(master)
        self.datafile = datafile
        self.pack()
        self.buildWindow()

 
    def buildWindow(self):
         
        self.master.title("Plot")

        bar = tkinter.Menu(self.master)
        fileMenu = tkinter.Menu(bar,tearoff=0)
            
        def loadFile(filename=None):

            if filename == None:
                filename = tkinter.filedialog.askopenfilename(title="Select a Plot File")
            
            theTurtle.clear()
            theTurtle.penup()
            theTurtle.goto(0,0)
            theTurtle.pendown()  
            screen.update()
            theTurtle.color("black")
            
            xmldoc = xml.dom.minidom.parse(filename)
                    
            plotElement = xmldoc.getElementsByTagName("Plot")[0]
            
            attr = plotElement.attributes
            self.master.title(attr["title"].value)

            axesElement = plotElement.getElementsByTagName("Axes")[0]
            
            xAxisElement = axesElement.getElementsByTagName("XAxis")[0]
            xAxisLabel = xAxisElement.firstChild.data.strip()
             
            yAxisElement = axesElement.getElementsByTagName("YAxis")[0]
            yAxisLabel = yAxisElement.firstChild.data.strip()
            
            xAttr = xAxisElement.attributes
            yAttr = yAxisElement.attributes
            
            minX = float(xAttr["min"].value)
            maxX = float(xAttr["max"].value)
            minY = float(yAttr["min"].value)
            maxY = float(yAttr["max"].value)
            
            xSize = maxX - minX
            ySize = maxY - minY
            xCenter = xSize / 2.0 + minX
            yCenter = ySize / 2.0 + minY
            
            xPlaces = max(4-round(math.log(xSize,10)),0)
            yPlaces = max(4-round(math.log(ySize,10)),0)
            
            labelYVal = maxY - 0.10 * ySize
            
            screen.setworldcoordinates(minX-0.20 * xSize,minY - 0.20 * ySize, \
                maxX + 0.20 * xSize,maxY + 0.20 * ySize)

            theTurtle.ht()
            
            theTurtle.penup()
            theTurtle.goto(minX,minY)
            theTurtle.pendown()
            theTurtle.goto(maxX,minY)
            theTurtle.penup()
            theTurtle.goto(minX,minY)
            theTurtle.pendown()
            theTurtle.goto(minX,maxY)
            theTurtle.penup()

            theTurtle.goto(xCenter, minY - ySize * 0.10)
            theTurtle.write(xAxisLabel,align="center",font=("Arial",14,"bold"))            

            theTurtle.goto(minX, maxY + 0.05 * ySize)
            theTurtle.write(yAxisLabel,align="center",font=("Arial",14,"bold"))
            
            for i in range(0,101,10):
                x = minX + xSize * i / 100.0
                y = minY + ySize * i / 100.0
                
                theTurtle.penup()
                theTurtle.goto(x,minY+ySize * 0.025)
                theTurtle.pendown()
                theTurtle.goto(x,minY-ySize * 0.025)
                theTurtle.penup()
                theTurtle.goto(x,minY-ySize * 0.05)
                
                theTurtle.write(("%1."+str(xPlaces)+"f")%x,align="center", \
                    font=("Arial",12,"normal"))
                
                theTurtle.penup()
                theTurtle.goto(minX+xSize * 0.025, y)
                theTurtle.pendown()
                theTurtle.goto(minX-xSize * 0.025, y)
                theTurtle.goto(minX-xSize * 0.001, y)
                theTurtle.write(("%1."+str(yPlaces)+"f")%y,align="right", \
                    font=("Arial",12,"normal"))
                 
            
            sequences = plotElement.getElementsByTagName("Sequence")
            
            for sequence in sequences:
                attr = sequence.attributes
                
                label = attr["title"].value.strip()
                color = attr["color"].value
                theTurtle.color(color)
                theTurtle.penup()
                theTurtle.goto(xCenter,labelYVal)
                labelYVal = labelYVal - 0.10 * ySize
                theTurtle.write(label,align="center",font=("Arial",14,"bold"))
                
                dataPoints = sequence.getElementsByTagName("DataPoint")
                
                first = dataPoints[0]
                attr = first.attributes
                x = float(attr["x"].value)
                y = float(attr["y"].value)
                theTurtle.goto(x,y)
                theTurtle.dot()
                theTurtle.pendown()
                
                for dataPoint in dataPoints:
                    attr = dataPoint.attributes
                    x = float(attr["x"].value)
                    y = float(attr["y"].value)
                    theTurtle.goto(x,y)
                    theTurtle.dot()
                    
                screen.update()

            
            
        fileMenu.add_command(label="Load Plot Data...",command=loadFile)
        
        fileMenu.add_command(label="Exit",command=self.master.quit)
        
        bar.add_cascade(label="File",menu=fileMenu)
        
        self.master.config(menu=bar)    
        
        canvas = tkinter.Canvas(self,width=1000,height=800)
        canvas.pack(side=tkinter.LEFT)
        
        theTurtle = turtle.RawTurtle(canvas)
         
        screen = theTurtle.getscreen()
        
        screen.tracer(0)

        if self.datafile != None:
            loadFile(self.datafile.strip())

def main():
    root = tkinter.Tk()
    datafile = None
    if len(sys.argv) > 1:
        datafile = sys.argv[1]
    plotApp = PlotApplication(root, datafile)  

    plotApp.mainloop()
    print("Program Execution Completed.")
        
if __name__ == "__main__":
    main()

¿Alguien me puede ayudar a descubrir por qué no me da la solución?

1
  • Buen día, te hace falta return seq en def selSort(seq):, no estás regresando nada y por lo tanto regresa None, supongo que eso afecta lo demás. Commented el 26 abr. 2022 a las 14:54

1 respuesta 1

0

Buen día,

Respuesta corta, tu código funciona correctamente.

Explicación del por qué ves 00:00:00 como resultado

Hice pruebas con tu código y lo reduje para poder mostrarte los resultados

Cuando tienes pocos valores en tu lista, el tiempo de ejecución para ordenarla es muy corto, por lo que te devuelve 00:00:00, es decir, el tiempo para ordenar la lista es prácticamente despreciable (Esto va a depender de tu PC).

Hice una prueba con el valor mínimo 10 y el máximo 200 para que puedas ver más o menos cuantos elementos tiene que tener tu lista para poder empezar a ver valores diferentes de cero

El código que usé para las pruebas es el siguiente:

import datetime 
import random
import time
from random import randint
from random import randrange

def select(seq, start):
    minIndex = start

    for j in range(start+1, len(seq)):
        if seq[minIndex] > seq[j]:
            minIndex = j

    return minIndex
    
def selSort(seq):
    for i in range(len(seq)-1):
        minIndex = select(seq, i)
        tmp = seq[i]
        seq[i] = seq[minIndex]
        seq[minIndex] = tmp
    
    return seq

#genero una lista de n números aleatorios
def listaAleatorios(n):
    lista = [0]  * n
    for i in range(n):
        lista[i] = random.randint(0, 1000)
    return lista

# Test lists of size 10 to 200
xmin = 10
xmax = 200

# Record the number sizes in xList and the average access time within
# a list that size in yList for 1000 retrievals. 
xList = []
yList = []

for x in range(xmin, xmax+1, 1):
    xList.append(x)

    # let any garbage collection/memory allocation complete or at least
    # settle down
    time.sleep(1)

    #genero una lista de números aleatorios de x elementos de la coordenada x
    aleatorios=listaAleatorios(x)
    #genero una lista de números aleatorios de x elementos de la coordenada y

    # Time before the 1000 test retrievals
    starttime = datetime.datetime.now()

    selSort(aleatorios)

    # Time after the 1000 test retrievals  
    endtime = datetime.datetime.now()

    # The difference in time between start and end.
    deltaT = endtime - starttime
    
    print(x, starttime, endtime, deltaT)

    # Divide by 1000 for the average access time
    # But also multiply by 1000000 for microseconds.
    accessTime = deltaT.total_seconds() 
    accessTime = accessTime * 1000

    yList.append(accessTime)

print(yList)

El resultado de la línea print(x, starttime, endtime, deltaT) es el siguiente (Nota: La fecha y hora que ves está definida por el lugar donde vivo, lo comento para que no creas que los datos son falsos):

10 2022-04-27 00:08:08.144028 2022-04-27 00:08:08.144028 0:00:00
11 2022-04-27 00:08:09.150599 2022-04-27 00:08:09.150599 0:00:00
12 2022-04-27 00:08:10.161806 2022-04-27 00:08:10.161806 0:00:00
13 2022-04-27 00:08:11.174805 2022-04-27 00:08:11.174805 0:00:00
14 2022-04-27 00:08:12.182090 2022-04-27 00:08:12.182090 0:00:00
15 2022-04-27 00:08:13.187492 2022-04-27 00:08:13.187492 0:00:00
16 2022-04-27 00:08:14.201413 2022-04-27 00:08:14.201413 0:00:00
17 2022-04-27 00:08:15.214096 2022-04-27 00:08:15.214096 0:00:00
18 2022-04-27 00:08:16.228759 2022-04-27 00:08:16.228759 0:00:00
19 2022-04-27 00:08:17.244088 2022-04-27 00:08:17.244088 0:00:00
20 2022-04-27 00:08:18.259034 2022-04-27 00:08:18.259034 0:00:00
21 2022-04-27 00:08:19.259144 2022-04-27 00:08:19.259144 0:00:00
22 2022-04-27 00:08:20.269186 2022-04-27 00:08:20.269186 0:00:00
23 2022-04-27 00:08:21.272421 2022-04-27 00:08:21.272421 0:00:00
24 2022-04-27 00:08:22.287164 2022-04-27 00:08:22.287164 0:00:00
25 2022-04-27 00:08:23.302032 2022-04-27 00:08:23.302032 0:00:00
26 2022-04-27 00:08:24.305249 2022-04-27 00:08:24.305249 0:00:00
27 2022-04-27 00:08:25.305335 2022-04-27 00:08:25.305335 0:00:00
28 2022-04-27 00:08:26.308137 2022-04-27 00:08:26.308137 0:00:00
29 2022-04-27 00:08:27.308912 2022-04-27 00:08:27.308912 0:00:00
30 2022-04-27 00:08:28.318666 2022-04-27 00:08:28.318666 0:00:00
31 2022-04-27 00:08:29.319634 2022-04-27 00:08:29.319634 0:00:00
32 2022-04-27 00:08:30.321760 2022-04-27 00:08:30.321760 0:00:00
33 2022-04-27 00:08:31.332353 2022-04-27 00:08:31.332353 0:00:00
34 2022-04-27 00:08:32.339496 2022-04-27 00:08:32.339496 0:00:00
35 2022-04-27 00:08:33.354765 2022-04-27 00:08:33.354765 0:00:00
36 2022-04-27 00:08:34.366695 2022-04-27 00:08:34.366695 0:00:00
37 2022-04-27 00:08:35.373413 2022-04-27 00:08:35.373413 0:00:00
38 2022-04-27 00:08:36.376021 2022-04-27 00:08:36.376021 0:00:00
39 2022-04-27 00:08:37.390173 2022-04-27 00:08:37.390173 0:00:00
40 2022-04-27 00:08:38.393770 2022-04-27 00:08:38.393770 0:00:00
41 2022-04-27 00:08:39.395724 2022-04-27 00:08:39.395724 0:00:00
42 2022-04-27 00:08:40.400734 2022-04-27 00:08:40.400734 0:00:00
43 2022-04-27 00:08:41.401882 2022-04-27 00:08:41.401882 0:00:00
44 2022-04-27 00:08:42.408748 2022-04-27 00:08:42.408748 0:00:00
45 2022-04-27 00:08:43.415463 2022-04-27 00:08:43.415463 0:00:00
46 2022-04-27 00:08:44.418884 2022-04-27 00:08:44.418884 0:00:00
47 2022-04-27 00:08:45.424023 2022-04-27 00:08:45.424023 0:00:00
48 2022-04-27 00:08:46.429608 2022-04-27 00:08:46.429608 0:00:00
49 2022-04-27 00:08:47.442395 2022-04-27 00:08:47.442395 0:00:00
50 2022-04-27 00:08:48.455276 2022-04-27 00:08:48.455276 0:00:00
51 2022-04-27 00:08:49.470246 2022-04-27 00:08:49.470246 0:00:00
52 2022-04-27 00:08:50.485382 2022-04-27 00:08:50.485382 0:00:00
53 2022-04-27 00:08:51.498610 2022-04-27 00:08:51.498610 0:00:00
54 2022-04-27 00:08:52.507171 2022-04-27 00:08:52.507171 0:00:00
55 2022-04-27 00:08:53.521602 2022-04-27 00:08:53.521602 0:00:00
56 2022-04-27 00:08:54.535608 2022-04-27 00:08:54.535608 0:00:00
57 2022-04-27 00:08:55.537490 2022-04-27 00:08:55.537490 0:00:00
58 2022-04-27 00:08:56.544389 2022-04-27 00:08:56.544389 0:00:00
59 2022-04-27 00:08:57.544780 2022-04-27 00:08:57.544780 0:00:00
60 2022-04-27 00:08:58.552389 2022-04-27 00:08:58.552389 0:00:00
61 2022-04-27 00:08:59.563723 2022-04-27 00:08:59.563723 0:00:00
62 2022-04-27 00:09:00.564144 2022-04-27 00:09:00.564144 0:00:00
63 2022-04-27 00:09:01.567108 2022-04-27 00:09:01.567108 0:00:00
64 2022-04-27 00:09:02.567357 2022-04-27 00:09:02.567357 0:00:00
65 2022-04-27 00:09:03.577841 2022-04-27 00:09:03.577841 0:00:00
66 2022-04-27 00:09:04.588612 2022-04-27 00:09:04.588612 0:00:00
67 2022-04-27 00:09:05.600439 2022-04-27 00:09:05.600439 0:00:00
68 2022-04-27 00:09:06.603789 2022-04-27 00:09:06.603789 0:00:00
69 2022-04-27 00:09:07.613089 2022-04-27 00:09:07.613089 0:00:00
70 2022-04-27 00:09:08.627907 2022-04-27 00:09:08.627907 0:00:00
71 2022-04-27 00:09:09.630216 2022-04-27 00:09:09.630216 0:00:00
72 2022-04-27 00:09:10.639033 2022-04-27 00:09:10.639033 0:00:00
73 2022-04-27 00:09:11.653755 2022-04-27 00:09:11.653755 0:00:00
74 2022-04-27 00:09:12.667698 2022-04-27 00:09:12.667698 0:00:00
75 2022-04-27 00:09:13.681992 2022-04-27 00:09:13.681992 0:00:00
76 2022-04-27 00:09:14.695019 2022-04-27 00:09:14.695019 0:00:00
77 2022-04-27 00:09:15.696692 2022-04-27 00:09:15.696692 0:00:00
78 2022-04-27 00:09:16.702494 2022-04-27 00:09:16.702494 0:00:00
79 2022-04-27 00:09:17.712656 2022-04-27 00:09:17.712656 0:00:00
80 2022-04-27 00:09:18.724939 2022-04-27 00:09:18.724939 0:00:00
81 2022-04-27 00:09:19.736455 2022-04-27 00:09:19.736455 0:00:00
82 2022-04-27 00:09:20.744011 2022-04-27 00:09:20.744011 0:00:00
83 2022-04-27 00:09:21.750139 2022-04-27 00:09:21.750139 0:00:00
84 2022-04-27 00:09:22.763997 2022-04-27 00:09:22.763997 0:00:00
85 2022-04-27 00:09:23.774604 2022-04-27 00:09:23.774604 0:00:00
86 2022-04-27 00:09:24.787679 2022-04-27 00:09:24.787679 0:00:00
87 2022-04-27 00:09:25.796289 2022-04-27 00:09:25.796289 0:00:00
88 2022-04-27 00:09:26.809962 2022-04-27 00:09:26.809962 0:00:00
89 2022-04-27 00:09:27.823431 2022-04-27 00:09:27.823431 0:00:00
90 2022-04-27 00:09:28.837953 2022-04-27 00:09:28.837953 0:00:00
91 2022-04-27 00:09:29.845871 2022-04-27 00:09:29.845871 0:00:00
92 2022-04-27 00:09:30.850708 2022-04-27 00:09:30.850708 0:00:00
93 2022-04-27 00:09:31.861609 2022-04-27 00:09:31.861609 0:00:00
94 2022-04-27 00:09:32.873616 2022-04-27 00:09:32.873616 0:00:00
95 2022-04-27 00:09:33.885700 2022-04-27 00:09:33.885700 0:00:00
96 2022-04-27 00:09:34.891730 2022-04-27 00:09:34.891730 0:00:00
97 2022-04-27 00:09:35.895628 2022-04-27 00:09:35.895628 0:00:00
98 2022-04-27 00:09:36.895747 2022-04-27 00:09:36.895747 0:00:00
99 2022-04-27 00:09:37.910366 2022-04-27 00:09:37.910366 0:00:00
100 2022-04-27 00:09:38.919590 2022-04-27 00:09:38.919590 0:00:00
101 2022-04-27 00:09:39.927612 2022-04-27 00:09:39.927612 0:00:00
102 2022-04-27 00:09:40.938149 2022-04-27 00:09:40.938149 0:00:00
103 2022-04-27 00:09:41.946585 2022-04-27 00:09:41.946585 0:00:00
104 2022-04-27 00:09:42.957600 2022-04-27 00:09:42.957600 0:00:00
105 2022-04-27 00:09:43.972276 2022-04-27 00:09:43.972276 0:00:00
106 2022-04-27 00:09:44.982595 2022-04-27 00:09:44.982595 0:00:00
107 2022-04-27 00:09:45.995608 2022-04-27 00:09:45.995608 0:00:00
108 2022-04-27 00:09:46.996571 2022-04-27 00:09:46.996571 0:00:00
109 2022-04-27 00:09:48.011216 2022-04-27 00:09:48.011216 0:00:00
110 2022-04-27 00:09:49.013585 2022-04-27 00:09:49.013585 0:00:00
111 2022-04-27 00:09:50.028836 2022-04-27 00:09:50.028836 0:00:00
112 2022-04-27 00:09:51.040568 2022-04-27 00:09:51.040568 0:00:00
113 2022-04-27 00:09:52.041877 2022-04-27 00:09:52.041877 0:00:00
114 2022-04-27 00:09:53.055098 2022-04-27 00:09:53.055098 0:00:00
115 2022-04-27 00:09:54.069682 2022-04-27 00:09:54.069682 0:00:00
116 2022-04-27 00:09:55.073223 2022-04-27 00:09:55.073223 0:00:00
117 2022-04-27 00:09:56.078771 2022-04-27 00:09:56.078771 0:00:00
118 2022-04-27 00:09:57.081966 2022-04-27 00:09:57.081966 0:00:00
119 2022-04-27 00:09:58.096992 2022-04-27 00:09:58.096992 0:00:00
120 2022-04-27 00:09:59.111469 2022-04-27 00:09:59.111469 0:00:00
121 2022-04-27 00:10:00.114936 2022-04-27 00:10:00.114936 0:00:00
122 2022-04-27 00:10:01.115475 2022-04-27 00:10:01.115475 0:00:00
123 2022-04-27 00:10:02.120447 2022-04-27 00:10:02.120447 0:00:00
124 2022-04-27 00:10:03.135235 2022-04-27 00:10:03.135235 0:00:00
125 2022-04-27 00:10:04.140664 2022-04-27 00:10:04.140664 0:00:00
126 2022-04-27 00:10:05.149079 2022-04-27 00:10:05.149079 0:00:00
127 2022-04-27 00:10:06.152461 2022-04-27 00:10:06.152461 0:00:00
128 2022-04-27 00:10:07.166896 2022-04-27 00:10:07.166896 0:00:00
129 2022-04-27 00:10:08.174878 2022-04-27 00:10:08.174878 0:00:00
130 2022-04-27 00:10:09.189584 2022-04-27 00:10:09.189584 0:00:00
131 2022-04-27 00:10:10.194038 2022-04-27 00:10:10.194038 0:00:00
132 2022-04-27 00:10:11.195798 2022-04-27 00:10:11.195798 0:00:00
133 2022-04-27 00:10:12.200087 2022-04-27 00:10:12.200087 0:00:00
134 2022-04-27 00:10:13.204132 2022-04-27 00:10:13.204132 0:00:00
135 2022-04-27 00:10:14.214188 2022-04-27 00:10:14.214188 0:00:00
136 2022-04-27 00:10:15.228938 2022-04-27 00:10:15.228938 0:00:00
137 2022-04-27 00:10:16.239149 2022-04-27 00:10:16.239149 0:00:00
138 2022-04-27 00:10:17.243858 2022-04-27 00:10:17.243858 0:00:00
139 2022-04-27 00:10:18.256524 2022-04-27 00:10:18.256524 0:00:00
140 2022-04-27 00:10:19.263685 2022-04-27 00:10:19.263685 0:00:00
141 2022-04-27 00:10:20.268071 2022-04-27 00:10:20.268071 0:00:00
142 2022-04-27 00:10:21.274296 2022-04-27 00:10:21.274296 0:00:00
143 2022-04-27 00:10:22.277323 2022-04-27 00:10:22.277323 0:00:00
144 2022-04-27 00:10:23.291629 2022-04-27 00:10:23.291629 0:00:00
145 2022-04-27 00:10:24.304845 2022-04-27 00:10:24.304845 0:00:00
146 2022-04-27 00:10:25.319542 2022-04-27 00:10:25.319542 0:00:00
147 2022-04-27 00:10:26.330476 2022-04-27 00:10:26.330476 0:00:00
148 2022-04-27 00:10:27.342473 2022-04-27 00:10:27.342473 0:00:00
149 2022-04-27 00:10:28.357574 2022-04-27 00:10:28.357574 0:00:00
150 2022-04-27 00:10:29.371490 2022-04-27 00:10:29.371490 0:00:00
151 2022-04-27 00:10:30.381025 2022-04-27 00:10:30.381025 0:00:00
152 2022-04-27 00:10:31.383896 2022-04-27 00:10:31.383896 0:00:00
153 2022-04-27 00:10:32.398600 2022-04-27 00:10:32.398600 0:00:00
154 2022-04-27 00:10:33.413125 2022-04-27 00:10:33.413125 0:00:00
155 2022-04-27 00:10:34.422597 2022-04-27 00:10:34.423250 0:00:00.000653
156 2022-04-27 00:10:35.433857 2022-04-27 00:10:35.434477 0:00:00.000620
157 2022-04-27 00:10:36.441488 2022-04-27 00:10:36.441488 0:00:00
158 2022-04-27 00:10:37.455456 2022-04-27 00:10:37.455456 0:00:00
159 2022-04-27 00:10:38.459800 2022-04-27 00:10:38.459800 0:00:00
160 2022-04-27 00:10:39.475042 2022-04-27 00:10:39.475042 0:00:00
161 2022-04-27 00:10:40.481304 2022-04-27 00:10:40.481304 0:00:00
162 2022-04-27 00:10:41.493370 2022-04-27 00:10:41.493370 0:00:00
163 2022-04-27 00:10:42.503701 2022-04-27 00:10:42.504710 0:00:00.001009
164 2022-04-27 00:10:43.516961 2022-04-27 00:10:43.516961 0:00:00
165 2022-04-27 00:10:44.520267 2022-04-27 00:10:44.520267 0:00:00
166 2022-04-27 00:10:45.524984 2022-04-27 00:10:45.524984 0:00:00
167 2022-04-27 00:10:46.525654 2022-04-27 00:10:46.525654 0:00:00
168 2022-04-27 00:10:47.527080 2022-04-27 00:10:47.527715 0:00:00.000635
169 2022-04-27 00:10:48.531409 2022-04-27 00:10:48.531409 0:00:00
170 2022-04-27 00:10:49.542945 2022-04-27 00:10:49.542945 0:00:00
171 2022-04-27 00:10:50.547421 2022-04-27 00:10:50.547421 0:00:00
172 2022-04-27 00:10:51.551312 2022-04-27 00:10:51.551312 0:00:00
173 2022-04-27 00:10:52.558002 2022-04-27 00:10:52.558002 0:00:00
174 2022-04-27 00:10:53.563467 2022-04-27 00:10:53.564117 0:00:00.000650
175 2022-04-27 00:10:54.564366 2022-04-27 00:10:54.564366 0:00:00
176 2022-04-27 00:10:55.579156 2022-04-27 00:10:55.579156 0:00:00
177 2022-04-27 00:10:56.583002 2022-04-27 00:10:56.583002 0:00:00
178 2022-04-27 00:10:57.591428 2022-04-27 00:10:57.591428 0:00:00
179 2022-04-27 00:10:58.604400 2022-04-27 00:10:58.604400 0:00:00
180 2022-04-27 00:10:59.610675 2022-04-27 00:10:59.611311 0:00:00.000636
181 2022-04-27 00:11:00.623129 2022-04-27 00:11:00.623129 0:00:00
182 2022-04-27 00:11:01.630658 2022-04-27 00:11:01.630658 0:00:00
183 2022-04-27 00:11:02.638380 2022-04-27 00:11:02.638380 0:00:00
184 2022-04-27 00:11:03.651502 2022-04-27 00:11:03.651502 0:00:00
185 2022-04-27 00:11:04.662353 2022-04-27 00:11:04.662353 0:00:00
186 2022-04-27 00:11:05.667167 2022-04-27 00:11:05.667167 0:00:00
187 2022-04-27 00:11:06.670074 2022-04-27 00:11:06.670074 0:00:00
188 2022-04-27 00:11:07.672853 2022-04-27 00:11:07.673872 0:00:00.001019
189 2022-04-27 00:11:08.685383 2022-04-27 00:11:08.685834 0:00:00.000451
190 2022-04-27 00:11:09.699219 2022-04-27 00:11:09.699859 0:00:00.000640
191 2022-04-27 00:11:10.705035 2022-04-27 00:11:10.705820 0:00:00.000785
192 2022-04-27 00:11:11.711393 2022-04-27 00:11:11.712414 0:00:00.001021
193 2022-04-27 00:11:12.719773 2022-04-27 00:11:12.720403 0:00:00.000630
194 2022-04-27 00:11:13.731281 2022-04-27 00:11:13.732302 0:00:00.001021
195 2022-04-27 00:11:14.747438 2022-04-27 00:11:14.748457 0:00:00.001019
196 2022-04-27 00:11:15.755858 2022-04-27 00:11:15.756876 0:00:00.001018
197 2022-04-27 00:11:16.761991 2022-04-27 00:11:16.763009 0:00:00.001018
198 2022-04-27 00:11:17.763472 2022-04-27 00:11:17.764491 0:00:00.001019
199 2022-04-27 00:11:18.770473 2022-04-27 00:11:18.771370 0:00:00.000897
200 2022-04-27 00:11:19.775479 2022-04-27 00:11:19.776489 0:00:00.001010

Como puedes ver, aún con 200 elementos, el tiempo de ejecución es únicamente 0:00:00.001010, es decir 1.01 milisegundos (ms)

Lo que devuelve yList es lo siguiente

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.653, 0.62, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.009, 0.0, 0.0, 0.0, 0.0, 0.635, 0.0, 0.0, 0.0, 0.0, 0.0, 0.65, 0.0, 0.0, 0.0, 0.0, 0.0, 0.636, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.019, 0.451, 0.64, 0.785, 1.021, 0.63, 1.021, 1.019, 1.018, 1.018, 1.019, 0.897, 1.01]

Tienes muy pocos elementos en tu lista como para que puedas ver el tiempo que tarda en ordenarse. Ahora, el por qué los valores no incrementan de forma lineal y de pronto puedes ver que un valor es mayor y el siguiente, a pesar de tener 1 elemento más es menor, eso es porque datetime.datetime.now() no es muy exacto

Si hacemos la misma prueba pero utilizando el módulo timeit puedes ver con mayor precisión el tiempo de ejecución

Sustituí datetime.datetime.now() por timeit.default_timer() e importe timeit (import timeit), este es el resultado

10 311977.4284619 311977.428473 1.1100026313215494e-05
11 311978.4309206 311978.4309311 1.0499963536858559e-05
12 311979.4391949 311979.4392067 1.1800031643360853e-05
13 311980.4433405 311980.443354 1.3499986380338669e-05
14 311981.4449551 311981.4449693 1.4199991710484028e-05
15 311982.4573085 311982.4573227 1.4199991710484028e-05
16 311983.4695452 311983.4695604 1.5199999324977398e-05
17 311984.4783188 311984.4783346 1.580000389367342e-05
18 311985.482987 311985.4830048 1.7799960914999247e-05
19 311986.4914863 311986.4915063 1.999997766688466e-05
20 311987.4987973 311987.498817 1.9700033590197563e-05
21 311988.5083044 311988.5083256 2.1199986804276705e-05
22 311989.523845 311989.5238675 2.2499996703118086e-05
23 311990.5245939 311990.5246209 2.6999972760677338e-05
24 311991.5366398 311991.5366643 2.4500011932104826e-05
25 311992.5500121 311992.5500387 2.659996971487999e-05
26 311993.5616449 311993.5616744 2.9499991796910763e-05
27 311994.5662931 311994.5663221 2.8999987989664078e-05
28 311995.5673212 311995.5673641 4.290003562346101e-05
29 311996.5746914 311996.5747235 3.2100011594593525e-05
30 311997.577847 311997.5778801 3.3100019209086895e-05
31 311998.5902756 311998.5903111 3.549997927621007e-05
32 311999.6022976 311999.6023469 4.929996794089675e-05
33 312000.6144902 312000.6145285 3.8300000596791506e-05
34 312001.6211256 312001.6211654 3.980001201853156e-05
35 312002.6292681 312002.6293096 4.1499966755509377e-05
36 312003.6307966 312003.6308398 4.3199979700148106e-05
37 312004.6362679 312004.6363135 4.5599997974932194e-05
38 312005.642918 312005.6429651 4.710000939667225e-05
39 312006.6536942 312006.6537432 4.900002386420965e-05
40 312007.6683435 312007.668394 5.0499977078288794e-05
41 312008.6715205 312008.6715733 5.2799994591623545e-05
42 312009.677776 312009.6778312 5.520001286640763e-05
43 312010.685969 312010.686026 5.70000265724957e-05
44 312011.6941951 312011.6942541 5.8999983593821526e-05
45 312012.6994275 312012.699488 6.049999501556158e-05
46 312013.7066636 312013.7067725 0.00010890001431107521
47 312014.714855 312014.71492 6.499997107312083e-05
48 312015.7230838 312015.7231493 6.550003308802843e-05
49 312016.7295472 312016.729616 6.880000000819564e-05
50 312017.7357692 312017.7358579 8.869997691363096e-05
51 312018.7429948 312018.7430673 7.250002818182111e-05
52 312019.7502122 312019.7502867 7.449998520314693e-05
53 312020.7574312 312020.7575094 7.82000133767724e-05
54 312021.7656834 312021.7657641 8.070003241300583e-05
55 312022.777597 312022.77768 8.299999171867967e-05
56 312023.7912645 312023.7913501 8.560001151636243e-05
57 312024.8062476 312024.8063369 8.929998148232698e-05
58 312025.8182184 312025.8183078 8.939998224377632e-05
59 312026.8208673 312026.8209605 9.320001117885113e-05
60 312027.8352017 312027.8352979 9.620003402233124e-05
61 312028.844545 312028.8446425 9.74999857135117e-05
62 312029.8592294 312029.8593301 0.00010070001007989049
63 312030.8702803 312030.8703837 0.00010340003063902259
64 312031.8852179 312031.8853237 0.00010579999070614576
65 312032.8977392 312032.8978527 0.00011350004933774471
66 312033.9088271 312033.9089407 0.00011359999189153314
67 312034.9093816 312034.9094961 0.00011449999874457717
68 312035.9225153 312035.922633 0.00011770002311095595
69 312036.935972 312036.9360921 0.00012009998317807913
70 312037.936939 312037.9370631 0.0001241000136360526
71 312038.9513254 312038.9514526 0.00012720003724098206
72 312039.9622677 312039.9623979 0.00013020000187680125
73 312040.9746363 312040.9747692 0.00013290002243593335
74 312041.9904109 312041.9905459 0.00013500003842636943
75 312042.9932666 312042.9934065 0.00013990001752972603
76 312044.0087051 312044.0088483 0.00014320004265755415
77 312045.0118988 312045.0121107 0.00021190004190430045
78 312046.0158933 312046.0160427 0.00014939997345209122
79 312047.0258821 312047.0260344 0.000152299995534122
80 312048.0277921 312048.0279487 0.00015660002827644348
81 312049.0317181 312049.0318778 0.00015969999367371202
82 312050.0422134 312050.0423776 0.00016419996973127127
83 312051.053916 312051.0540833 0.00016729999333620071
84 312052.0552037 312052.055377 0.00017330003902316093
85 312053.0682023 312053.0683753 0.000172999978531152
86 312054.0832088 312054.0833855 0.00017670000670477748
87 312055.0912363 312055.0914213 0.00018500001169741154
88 312056.0986348 312056.0988258 0.00019099999917671084
89 312057.1065074 312057.1066993 0.00019190000602975488
90 312058.1070452 312058.1072372 0.00019200000679120421
91 312059.1136748 312059.1138714 0.0001965999836102128
92 312060.1264259 312060.1266839 0.00025800004368647933
93 312061.1411248 312061.1413279 0.0002030999748967588
94 312062.1504706 312062.1506786 0.0002080000122077763
95 312063.1656648 312063.1658768 0.0002120000426657498
96 312064.1738439 312064.1740582 0.00021430000197142363
97 312065.1820606 312065.1823488 0.00028819998260587454
98 312066.1883059 312066.1885291 0.0002231999533250928
99 312067.1945308 312067.1947582 0.00022740004351362586
100 312068.2017669 312068.2019983 0.0002313999575562775
101 312069.2088468 312069.2090842 0.00023740000324323773
102 312070.2160574 312070.2163001 0.00024269998539239168
103 312071.2233377 312071.2237858 0.00044809997780248523
104 312072.2304966 312072.2308233 0.00032670004293322563
105 312073.2377248 312073.2379771 0.0002523000002838671
106 312074.2457903 312074.2460465 0.00025619997177273035
107 312075.2530142 312075.2532751 0.0002609000075608492
108 312076.2602313 312076.2605828 0.00035149999894201756
109 312077.2674741 312077.2677441 0.00027000001864507794
110 312078.2806432 312078.2809208 0.00027760001830756664
111 312079.2823303 312079.2826133 0.00028300000121816993
112 312080.2942673 312080.2945604 0.00029310001991689205
113 312081.3045292 312081.304823 0.0002937999670393765
114 312082.3125743 312082.3129634 0.0003890999942086637
115 312083.3275078 312083.3278118 0.00030399998649954796
116 312084.3366601 312084.3369682 0.0003081000177189708
117 312085.346939 312085.3472467 0.00030770001467317343
118 312086.3608239 312086.3611355 0.00031160004436969757
119 312087.3746375 312087.3749577 0.00032020005164667964
120 312088.3854753 312088.3858015 0.00032619998091831803
121 312089.3878023 312089.3881297 0.0003273999900557101
122 312090.387851 312090.3881827 0.00033169996459037066
123 312091.4027175 312091.4030554 0.00033790001180022955
124 312092.4144329 312092.414779 0.00034609995782375336
125 312093.4165936 312093.4169416 0.00034799997229129076
126 312094.4171719 312094.417524 0.0003521000035107136
127 312095.4302121 312095.430572 0.00035990000469610095
128 312096.4441341 312096.4444984 0.00036429997999221087
129 312097.4545552 312097.4549289 0.00037369999336078763
130 312098.4573441 312098.4577191 0.000375000003259629
131 312099.4719808 312099.4723573 0.00037650001468136907
132 312100.486853 312100.4872383 0.0003852999652735889
133 312101.4920987 312101.4924895 0.0003907999489456415
134 312102.5003601 312102.5007556 0.00039549998473376036
135 312103.5085588 312103.5091427 0.0005839000223204494
136 312104.5167533 312104.5171595 0.0004062000080011785
137 312105.52503 312105.5257796 0.0007496000034734607
138 312106.5312021 312106.5316194 0.000417300034314394
139 312107.5394524 312107.5398773 0.0004248999757692218
140 312108.5467062 312108.547481 0.0007748000207357109
141 312109.5548844 312109.5553279 0.00044350000098347664
142 312110.5601037 312110.5605431 0.00043940002797171474
143 312111.5682705 312111.5687192 0.00044869998237118125
144 312112.5758353 312112.5762889 0.00045359996147453785
145 312113.5830501 312113.5835091 0.00045900000259280205
146 312114.5882898 312114.5887526 0.00046280003152787685
147 312115.5955064 312115.5959743 0.00046790001215413213
148 312116.6037362 312116.6042115 0.00047529995208606124
149 312117.6119478 312117.6124289 0.0004811000544577837
150 312118.6191846 312118.6196704 0.0004857999738305807
151 312119.6264105 312119.6269045 0.0004940000362694263
152 312120.6340281 312120.6345282 0.000500100024510175
153 312121.6477603 312121.6482651 0.0005048000020906329
154 312122.6618002 312122.6623129 0.0005127000040374696
155 312123.6690776 312123.669596 0.0005183999892324209
156 312124.6833046 312124.6838294 0.0005247999797575176
157 312125.6895269 312125.6900582 0.0005312999710440636
158 312126.6977944 312126.6983315 0.0005371000152081251
159 312127.7047255 312127.7052679 0.0005423999973572791
160 312128.7119277 312128.7124794 0.0005517000099644065
161 312129.7201534 312129.7207095 0.0005561000434681773
162 312130.7264415 312130.7274734 0.0010318999993614852
163 312131.7340599 312131.7346296 0.0005697000306099653
164 312132.7460807 312132.746657 0.0005762999644502997
165 312133.7475177 312133.7481174 0.0005996999680064619
166 312134.7571267 312134.7577151 0.0005883999983780086
167 312135.7575316 312135.7581295 0.0005979000125080347
168 312136.76581 312136.7664121 0.000602099986281246
169 312137.7729732 312137.7735814 0.0006081999745219946
170 312138.7792223 312138.7798405 0.0006181999924592674
171 312139.7874509 312139.7880737 0.000622799969278276
172 312140.7956812 312140.7963136 0.0006323999841697514
173 312141.8036484 312141.8042853 0.0006369000184349716
174 312142.8182801 312142.8189289 0.0006487999926321208
175 312143.8232735 312143.8239232 0.0006496999994851649
176 312144.8360418 312144.836701 0.000659200013615191
177 312145.8490626 312145.8497309 0.0006683000246994197
178 312146.8650331 312146.8657058 0.0006726999999955297
179 312147.8760329 312147.8767125 0.000679599994327873
180 312148.8790561 312148.8797413 0.0006852000369690359
181 312149.8911795 312149.8918735 0.0006940000457689166
182 312150.9020304 312150.9027302 0.0006997999735176563
183 312151.9050472 312151.9057573 0.0007100999937392771
184 312152.9090953 312152.9098097 0.0007143999682739377
185 312153.9243627 312153.9250853 0.0007225999725051224
186 312154.9315762 312154.9323052 0.00072900002123788
187 312155.9378115 312155.9385512 0.0007396999862976372
188 312156.9440377 312156.9447822 0.0007444999646395445
189 312157.9522675 312157.9530206 0.0007531000301241875
190 312158.9605632 312158.9619602 0.0013969999854452908
191 312159.9677303 312159.9685097 0.0007793999975547194
192 312160.9739406 312160.9747166 0.000775999971665442
193 312161.9876108 312161.9884027 0.0007918999763205647
194 312162.9904856 312162.9912808 0.0007952000014483929
195 312163.9976993 312163.9985017 0.0008023999980650842
196 312165.0049534 312165.0057655 0.000812100013718009
197 312166.012156 312166.0129727 0.0008166999905370176
198 312167.0183999 312167.0192235 0.0008235999848693609
199 312168.0245827 312168.025416 0.0008333000005222857
200 312169.032019 312169.0328635 0.0008445000275969505

Las unidades de la última columna están en fracciones de segundo (Según la documentación) y eso quiere decir que al leerlo son segundos, por ejemplo, el último valor es 0.0008445000275969505 segundos, es decir, 844.5 microsegundos

Si te fijas para la misma prueba hay una diferencia de tiempo considerable, te recomiendo no utilizar datetime.datetime.now() para hacer pruebas de tiempo de ejecución de código (Otra alternativa a timeit es el módulo time)

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.