Tôi có một tập lệnh Python xử lý một số lượng lớn các tệp.
Đối với mỗi tệp, tập lệnh đi từng dòng, tìm kiếm các patter regex cụ thể. Nếu một mẫu được tìm thấy, dòng được sao chép vào tệp nhật ký.
Theo đầu vào mẫu, tôi chuyển nó một thư mục với 42 tệp nhỏ và 3 tệp lớn [mỗi dòng ~ 1500].
Các tập lệnh xử lý hai tệp lớn đầu tiên rất nhanh - nó cần một vài giây cho chúng. Nhưng khi nó đạt đến tệp lớn thứ ba, nó chậm lại, và nó sẽ chậm hơn và chậm hơn.
Ở giữa tập tin lớn thứ ba, nó cần toàn bộ thứ hai trên mỗi dòng và nó tiếp tục chậm lại. Nếu tôi không dừng nó, toàn bộ cuộc chạy mất một giờ!
Tôi đã thêm mã gỡ lỗi in ra các số dòng - đó là cách tôi nhận thấy rằng nó tiếp tục khuấy động chậm hơn và chậm hơn, và nó không bị mắc kẹt ở đâu đó.
Tôi có 20 năm kinh nghiệm với C và nhiều ngôn ngữ khác, nhưng tôi là người mới bắt đầu Python. Tôi có thể thực hiện các bước để khắc phục sự cố tập lệnh này là gì?
Tôi có một mô phỏng chạy có cấu trúc cơ bản này:
Nội dung chính ShowShow
- Tại sao chương trình Python của tôi lại chậm như vậy?
- Làm cách nào để làm cho tập lệnh Python của tôi chạy nhanh hơn?
- Python có làm chậm máy tính của bạn không?
- Một chương trình Python có thể chạy mãi mãi không?
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útL = 0
,a = 99
: 1,7 phútL = 2
,a = 1
: 2,7 phútL = 2
,a = 99
: 5,15 phút
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"]
a = 1
: 4,5 phút
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]######### 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 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?
Làm cách nào để làm cho tập lệnh Python của tôi chạy nhanh hơn?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?
Python có làm chậm máy tính của bạn không?.
Một chương trình Python có thể chạy mãi mãi không?
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]Python có làm chậm máy tính của bạn không?
Một chương trình Python có thể chạy mãi mãi không?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?
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].. Also, time. sleep is used to suspend the operation of a script for a period of time.