Hướng dẫn python program gets slower over time - chương trình python chậm hơn theo thời gian

Tôi có một mô phỏng chạy có cấu trúc cơ bản này:

from time import time

def CSV[*args]:
    #write * args to .CSV file
    return

def timeleft[a,L,period]:
    print[#details on how long last period took, ETA#]

for L in range[0,6,4]:
    for a in range[1,100]:
        timeA = time[]

            for t in range[1,1000]:

                ## Manufacturer in Supply Chain ##

                inventory_accounting_lists.append[#simple calculations#]

                    # Simulation to determine the optimal B-value [Basestock level]

                    for B in range[1,100]:
                        for tau in range[1,1000]:
                                ## simple inventory accounting operations##

                ## Distributor in Supply Chain ##

                inventory_accounting_lists.append[#simple calculations#]

                    # Simulation to determine the optimal B-value [Basestock level]

                    for B in range[1,100]:
                        for tau in range[1,1000]:
                                ## simple inventory accounting operations##

                ## Wholesaler in Supply Chain ##

                inventory_accounting_lists.append[#simple calculations#]

                    # Simulation to determine the optimal B-value [Basestock level]

                    for B in range[1,100]:
                        for tau in range[1,1000]:
                                ## simple inventory accounting operations##

                ## Retailer in Supply Chain ##

                inventory_accounting_lists.append[#simple calculations#]

                    # Simulation to determine the optimal B-value [Basestock level]

                    for B in range[1,100]:
                        for tau in range[1,1000]:
                                ## simple inventory accounting operations##


        CSV[Simulation_Results]

        timeB = time[]

        timeleft[a,L,timeB-timeA]

Khi kịch bản tiếp tục, nó dường như ngày càng chậm hơn. Đây là những gì nó là cho các giá trị này [và nó tăng tuyến tính khi tăng].

  • L = 0, a = 1: 1,15 phút
  • L = 0, a = 99: 1,7 phút
  • L = 2, a = 1: 2,7 phút
  • L = 2, a = 99: 5,15 phút
  • #########
    a = 0.01
    L = 0
    total = 1000
    sim = 500
    inv_cost = 1
    bl_cost = 4
    #########
    
    # Functions
    
    import random
    from time import time
    time0 = time[]
    
    # function to report ETA etc.
    
    def timeleft[a,L,period_time]:
        if L==0:
            periods_left = [[1-a]*100]-1+2*99
        if L==2:
            periods_left = [[1-a]*100]-1+99
        if L==4:
            periods_left = [[1-a]*100]-1+0*99
    
        minute_time = period_time/60
    
        minutes_left = [periods_left*period_time]/60
        hours_left = [periods_left*period_time]/3600
        percentage_complete = 100*[[297-periods_left]/297]
    
        print["Time for last period = ","%.2f" % minute_time," minutes"]
    
        print["%.2f" % percentage_complete,"% complete"]
        if hours_left 0:
                    O_4.append[B_4[t] - IP_4[t]]
                else:
                    O_4.append[0]
    
    
    
    
                #### DISTRIBUTOR ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_3[t-1]20:
                            break
                    else:
                        Run_3.append[0]
    
                # Use minimum cost as new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_3]]
                optimal_B = var[1]
                B_3.append[optimal_B]
    
                # Calculate O[t]
                if B_3[t] - IP_3[t] > 0:
                    O_3.append[B_3[t] - IP_3[t]]
                else:
                    O_3.append[0]
    
    
    
                #### WHOLESALER ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_2[t-1]20:
                            break
                    else:
                        Run_2.append[0]
    
                # Use minimum cost as new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_2]]
                optimal_B = var[1]
                B_2.append[optimal_B]
    
                # Calculate O[t]
                if B_2[t] - IP_2[t] > 0:
                    O_2.append[B_2[t] - IP_2[t]]
                else:
                    O_2.append[0]
    
    
    
    
    
                #### RETAILER ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_1[t-1]20:
                            break
                    else:
                        Run_1.append[0]
    
                # Use minimum as your new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_1]]
                optimal_B = var[1]
                B_1.append[optimal_B]
    
                # Calculate O[t]
                if B_1[t] - IP_1[t] > 0:
                    O_1.append[B_1[t] - IP_1[t]]
                else:
                    O_1.append[0]
    
    
            ### Calculate the Standard Devation of the last half of time periods ###
    
            def STD[numbers]:
                k = len[numbers]
                mean = sum[numbers] / k
                SD = [sum[[dev*dev for dev in [x-mean for x in numbers]]]/[k-1]]**.5
                return SD
    
            start = [total//2]+1
    
            # Only use the last half of the time periods to calculate the standard deviation
    
            I_STD_1_L.append[STD[I_1[start:]]]
            I_STD_2_L.append[STD[I_2[start:]]]
            I_STD_3_L.append[STD[I_3[start:]]]
            I_STD_4_L.append[STD[I_4[start:]]]
    
            O_STD_0_L.append[STD[D[start:]]]
            O_STD_1_L.append[STD[O_1[start:]]]
            O_STD_2_L.append[STD[O_2[start:]]]
            O_STD_3_L.append[STD[O_3[start:]]]
            O_STD_4_L.append[STD[O_4[start:]]]
    
            from time import time
            timeB = time[]
    
            timeleft[a,L,timeB-timeA]
    
            I_STD_1[L//2] = I_STD_1_L[:]
            I_STD_2[L//2] = I_STD_2_L[:]
            I_STD_3[L//2] = I_STD_3_L[:]
            I_STD_4[L//2] = I_STD_4_L[:]
    
            O_STD_0[L//2] = O_STD_0_L[:]
            O_STD_1[L//2] = O_STD_1_L[:]
            O_STD_2[L//2] = O_STD_2_L[:]
            O_STD_3[L//2] = O_STD_3_L[:]
            O_STD_4[L//2] = O_STD_4_L[:]
    
            CSV[a,L,I_STD_1,I_STD_2,I_STD_3,I_STD_4,O_STD_0,
                O_STD_1,O_STD_2,O_STD_3,O_STD_4]
    
    
    from time import time
    timeE = time[]
    
    print["Run Time: ",[timeE-time0]/3600," hours"]
    
    0, a = 1: 4,5 phút
  • #########
    a = 0.01
    L = 0
    total = 1000
    sim = 500
    inv_cost = 1
    bl_cost = 4
    #########
    
    # Functions
    
    import random
    from time import time
    time0 = time[]
    
    # function to report ETA etc.
    
    def timeleft[a,L,period_time]:
        if L==0:
            periods_left = [[1-a]*100]-1+2*99
        if L==2:
            periods_left = [[1-a]*100]-1+99
        if L==4:
            periods_left = [[1-a]*100]-1+0*99
    
        minute_time = period_time/60
    
        minutes_left = [periods_left*period_time]/60
        hours_left = [periods_left*period_time]/3600
        percentage_complete = 100*[[297-periods_left]/297]
    
        print["Time for last period = ","%.2f" % minute_time," minutes"]
    
        print["%.2f" % percentage_complete,"% complete"]
        if hours_left 0:
                    O_4.append[B_4[t] - IP_4[t]]
                else:
                    O_4.append[0]
    
    
    
    
                #### DISTRIBUTOR ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_3[t-1]20:
                            break
                    else:
                        Run_3.append[0]
    
                # Use minimum cost as new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_3]]
                optimal_B = var[1]
                B_3.append[optimal_B]
    
                # Calculate O[t]
                if B_3[t] - IP_3[t] > 0:
                    O_3.append[B_3[t] - IP_3[t]]
                else:
                    O_3.append[0]
    
    
    
                #### WHOLESALER ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_2[t-1]20:
                            break
                    else:
                        Run_2.append[0]
    
                # Use minimum cost as new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_2]]
                optimal_B = var[1]
                B_2.append[optimal_B]
    
                # Calculate O[t]
                if B_2[t] - IP_2[t] > 0:
                    O_2.append[B_2[t] - IP_2[t]]
                else:
                    O_2.append[0]
    
    
    
    
    
                #### RETAILER ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_1[t-1]20:
                            break
                    else:
                        Run_1.append[0]
    
                # Use minimum as your new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_1]]
                optimal_B = var[1]
                B_1.append[optimal_B]
    
                # Calculate O[t]
                if B_1[t] - IP_1[t] > 0:
                    O_1.append[B_1[t] - IP_1[t]]
                else:
                    O_1.append[0]
    
    
            ### Calculate the Standard Devation of the last half of time periods ###
    
            def STD[numbers]:
                k = len[numbers]
                mean = sum[numbers] / k
                SD = [sum[[dev*dev for dev in [x-mean for x in numbers]]]/[k-1]]**.5
                return SD
    
            start = [total//2]+1
    
            # Only use the last half of the time periods to calculate the standard deviation
    
            I_STD_1_L.append[STD[I_1[start:]]]
            I_STD_2_L.append[STD[I_2[start:]]]
            I_STD_3_L.append[STD[I_3[start:]]]
            I_STD_4_L.append[STD[I_4[start:]]]
    
            O_STD_0_L.append[STD[D[start:]]]
            O_STD_1_L.append[STD[O_1[start:]]]
            O_STD_2_L.append[STD[O_2[start:]]]
            O_STD_3_L.append[STD[O_3[start:]]]
            O_STD_4_L.append[STD[O_4[start:]]]
    
            from time import time
            timeB = time[]
    
            timeleft[a,L,timeB-timeA]
    
            I_STD_1[L//2] = I_STD_1_L[:]
            I_STD_2[L//2] = I_STD_2_L[:]
            I_STD_3[L//2] = I_STD_3_L[:]
            I_STD_4[L//2] = I_STD_4_L[:]
    
            O_STD_0[L//2] = O_STD_0_L[:]
            O_STD_1[L//2] = O_STD_1_L[:]
            O_STD_2[L//2] = O_STD_2_L[:]
            O_STD_3[L//2] = O_STD_3_L[:]
            O_STD_4[L//2] = O_STD_4_L[:]
    
            CSV[a,L,I_STD_1,I_STD_2,I_STD_3,I_STD_4,O_STD_0,
                O_STD_1,O_STD_2,O_STD_3,O_STD_4]
    
    
    from time import time
    timeE = time[]
    
    print["Run Time: ",[timeE-time0]/3600," hours"]
    
    0,
    #########
    a = 0.01
    L = 0
    total = 1000
    sim = 500
    inv_cost = 1
    bl_cost = 4
    #########
    
    # Functions
    
    import random
    from time import time
    time0 = time[]
    
    # function to report ETA etc.
    
    def timeleft[a,L,period_time]:
        if L==0:
            periods_left = [[1-a]*100]-1+2*99
        if L==2:
            periods_left = [[1-a]*100]-1+99
        if L==4:
            periods_left = [[1-a]*100]-1+0*99
    
        minute_time = period_time/60
    
        minutes_left = [periods_left*period_time]/60
        hours_left = [periods_left*period_time]/3600
        percentage_complete = 100*[[297-periods_left]/297]
    
        print["Time for last period = ","%.2f" % minute_time," minutes"]
    
        print["%.2f" % percentage_complete,"% complete"]
        if hours_left 0:
                    O_4.append[B_4[t] - IP_4[t]]
                else:
                    O_4.append[0]
    
    
    
    
                #### DISTRIBUTOR ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_3[t-1]20:
                            break
                    else:
                        Run_3.append[0]
    
                # Use minimum cost as new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_3]]
                optimal_B = var[1]
                B_3.append[optimal_B]
    
                # Calculate O[t]
                if B_3[t] - IP_3[t] > 0:
                    O_3.append[B_3[t] - IP_3[t]]
                else:
                    O_3.append[0]
    
    
    
                #### WHOLESALER ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_2[t-1]20:
                            break
                    else:
                        Run_2.append[0]
    
                # Use minimum cost as new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_2]]
                optimal_B = var[1]
                B_2.append[optimal_B]
    
                # Calculate O[t]
                if B_2[t] - IP_2[t] > 0:
                    O_2.append[B_2[t] - IP_2[t]]
                else:
                    O_2.append[0]
    
    
    
    
    
                #### RETAILER ####
    
                #recieve shipment from supplier, calculate items OH HAND
                if I_1[t-1]20:
                            break
                    else:
                        Run_1.append[0]
    
                # Use minimum as your new B[t]
                var = min[[val, idx] for [idx, val] in enumerate[S_BC_1]]
                optimal_B = var[1]
                B_1.append[optimal_B]
    
                # Calculate O[t]
                if B_1[t] - IP_1[t] > 0:
                    O_1.append[B_1[t] - IP_1[t]]
                else:
                    O_1.append[0]
    
    
            ### Calculate the Standard Devation of the last half of time periods ###
    
            def STD[numbers]:
                k = len[numbers]
                mean = sum[numbers] / k
                SD = [sum[[dev*dev for dev in [x-mean for x in numbers]]]/[k-1]]**.5
                return SD
    
            start = [total//2]+1
    
            # Only use the last half of the time periods to calculate the standard deviation
    
            I_STD_1_L.append[STD[I_1[start:]]]
            I_STD_2_L.append[STD[I_2[start:]]]
            I_STD_3_L.append[STD[I_3[start:]]]
            I_STD_4_L.append[STD[I_4[start:]]]
    
            O_STD_0_L.append[STD[D[start:]]]
            O_STD_1_L.append[STD[O_1[start:]]]
            O_STD_2_L.append[STD[O_2[start:]]]
            O_STD_3_L.append[STD[O_3[start:]]]
            O_STD_4_L.append[STD[O_4[start:]]]
    
            from time import time
            timeB = time[]
    
            timeleft[a,L,timeB-timeA]
    
            I_STD_1[L//2] = I_STD_1_L[:]
            I_STD_2[L//2] = I_STD_2_L[:]
            I_STD_3[L//2] = I_STD_3_L[:]
            I_STD_4[L//2] = I_STD_4_L[:]
    
            O_STD_0[L//2] = O_STD_0_L[:]
            O_STD_1[L//2] = O_STD_1_L[:]
            O_STD_2[L//2] = O_STD_2_L[:]
            O_STD_3[L//2] = O_STD_3_L[:]
            O_STD_4[L//2] = O_STD_4_L[:]
    
            CSV[a,L,I_STD_1,I_STD_2,I_STD_3,I_STD_4,O_STD_0,
                O_STD_1,O_STD_2,O_STD_3,O_STD_4]
    
    
    from time import time
    timeE = time[]
    
    print["Run Time: ",[timeE-time0]/3600," hours"]
    
    3: 4,95 phút [đây là giá trị mới nhất mà nó đã đạt được]

Tại sao mỗi lần lặp lại mất nhiều thời gian hơn? Mỗi lần lặp của vòng lặp về cơ bản đặt lại mọi thứ ngoại trừ một danh sách toàn cầu, được thêm vào mỗi lần. Tuy nhiên, các vòng lặp bên trong mỗi "giai đoạn" không truy cập vào danh sách chính này - họ đang truy cập cùng một danh sách địa phương mỗi lần.

EDIT 1: Tôi sẽ đăng mã mô phỏng ở đây, trong trường hợp bất cứ ai muốn lội qua nó, nhưng tôi cảnh báo bạn, nó khá dài và các tên biến có thể khó hiểu một cách không cần thiết.

#########
a = 0.01
L = 0
total = 1000
sim = 500
inv_cost = 1
bl_cost = 4
#########

# Functions

import random
from time import time
time0 = time[]

# function to report ETA etc.

def timeleft[a,L,period_time]:
    if L==0:
        periods_left = [[1-a]*100]-1+2*99
    if L==2:
        periods_left = [[1-a]*100]-1+99
    if L==4:
        periods_left = [[1-a]*100]-1+0*99

    minute_time = period_time/60

    minutes_left = [periods_left*period_time]/60
    hours_left = [periods_left*period_time]/3600
    percentage_complete = 100*[[297-periods_left]/297]

    print["Time for last period = ","%.2f" % minute_time," minutes"]

    print["%.2f" % percentage_complete,"% complete"]
    if hours_left 0:
                O_4.append[B_4[t] - IP_4[t]]
            else:
                O_4.append[0]




            #### DISTRIBUTOR ####

            #recieve shipment from supplier, calculate items OH HAND
            if I_3[t-1]20:
                        break
                else:
                    Run_3.append[0]

            # Use minimum cost as new B[t]
            var = min[[val, idx] for [idx, val] in enumerate[S_BC_3]]
            optimal_B = var[1]
            B_3.append[optimal_B]

            # Calculate O[t]
            if B_3[t] - IP_3[t] > 0:
                O_3.append[B_3[t] - IP_3[t]]
            else:
                O_3.append[0]



            #### WHOLESALER ####

            #recieve shipment from supplier, calculate items OH HAND
            if I_2[t-1]20:
                        break
                else:
                    Run_2.append[0]

            # Use minimum cost as new B[t]
            var = min[[val, idx] for [idx, val] in enumerate[S_BC_2]]
            optimal_B = var[1]
            B_2.append[optimal_B]

            # Calculate O[t]
            if B_2[t] - IP_2[t] > 0:
                O_2.append[B_2[t] - IP_2[t]]
            else:
                O_2.append[0]





            #### RETAILER ####

            #recieve shipment from supplier, calculate items OH HAND
            if I_1[t-1]20:
                        break
                else:
                    Run_1.append[0]

            # Use minimum as your new B[t]
            var = min[[val, idx] for [idx, val] in enumerate[S_BC_1]]
            optimal_B = var[1]
            B_1.append[optimal_B]

            # Calculate O[t]
            if B_1[t] - IP_1[t] > 0:
                O_1.append[B_1[t] - IP_1[t]]
            else:
                O_1.append[0]


        ### Calculate the Standard Devation of the last half of time periods ###

        def STD[numbers]:
            k = len[numbers]
            mean = sum[numbers] / k
            SD = [sum[[dev*dev for dev in [x-mean for x in numbers]]]/[k-1]]**.5
            return SD

        start = [total//2]+1

        # Only use the last half of the time periods to calculate the standard deviation

        I_STD_1_L.append[STD[I_1[start:]]]
        I_STD_2_L.append[STD[I_2[start:]]]
        I_STD_3_L.append[STD[I_3[start:]]]
        I_STD_4_L.append[STD[I_4[start:]]]

        O_STD_0_L.append[STD[D[start:]]]
        O_STD_1_L.append[STD[O_1[start:]]]
        O_STD_2_L.append[STD[O_2[start:]]]
        O_STD_3_L.append[STD[O_3[start:]]]
        O_STD_4_L.append[STD[O_4[start:]]]

        from time import time
        timeB = time[]

        timeleft[a,L,timeB-timeA]

        I_STD_1[L//2] = I_STD_1_L[:]
        I_STD_2[L//2] = I_STD_2_L[:]
        I_STD_3[L//2] = I_STD_3_L[:]
        I_STD_4[L//2] = I_STD_4_L[:]

        O_STD_0[L//2] = O_STD_0_L[:]
        O_STD_1[L//2] = O_STD_1_L[:]
        O_STD_2[L//2] = O_STD_2_L[:]
        O_STD_3[L//2] = O_STD_3_L[:]
        O_STD_4[L//2] = O_STD_4_L[:]

        CSV[a,L,I_STD_1,I_STD_2,I_STD_3,I_STD_4,O_STD_0,
            O_STD_1,O_STD_2,O_STD_3,O_STD_4]


from time import time
timeE = time[]

print["Run Time: ",[timeE-time0]/3600," hours"]

Tại sao chương trình Python của tôi lại chậm như vậy?

Tóm lại: mã bị chậm lại bởi việc biên dịch và giải thích xảy ra trong thời gian chạy. So sánh điều này với một ngôn ngữ được gõ tĩnh, được biên dịch chỉ chạy các hướng dẫn CPU một khi được tổng hợp. Thực sự có thể mở rộng Python với các mô -đun được biên dịch được viết bằng C.code is slowed down by the compilation and interpretation that occurs during runtime. Compare this to a statically typed, compiled language which runs just the CPU instructions once compilated. It's actually possible to extend Python with compiled modules that are written in C.

Làm cách nào để làm cho tập lệnh Python của tôi chạy nhanh hơn?

Tài liệu nền tảng nhận dạng Loginradius..
Một vài cách để tăng tốc mã Python của bạn ..
Sử dụng cấu trúc dữ liệu thích hợp ..
Giảm sử dụng cho vòng lặp ..
Sử dụng danh sách hiểu ..
Sử dụng nhiều bài tập ..
Không sử dụng các biến toàn cầu ..
Sử dụng chức năng thư viện ..
Chuỗi Concatenate với tham gia ..

Python có làm chậm máy tính của bạn không?

Mặc dù có nhiều phẩm chất này, Python có một nhược điểm, đó là tốc độ chậm.Là một ngôn ngữ được giải thích, Python chậm hơn các ngôn ngữ lập trình khác.Tuy nhiên, chúng ta có thể khắc phục vấn đề này bằng một số mẹo.it's slow speed. Being an interpreted language, python is slower than other programming languages. Still, we can overcome this problem using some tips.

Một chương trình Python có thể chạy mãi mãi không?

Có, bạn có thể sử dụng một thời gian đúng: Vòng lặp không bao giờ phá vỡ để chạy mã Python liên tục.Ngoài ra, thời gian.Giấc ngủ được sử dụng để đình chỉ hoạt động của một kịch bản trong một khoảng thời gian.. Also, time. sleep is used to suspend the operation of a script for a period of time.

Bài Viết Liên Quan

Chủ Đề