2014-03-07 35 views
3

我有一個非常大的代碼,需要一些時間才能運行。爲了確保進程沒有停滯在某個地方,我打印屏幕上已經執行的代碼的百分比,這取決於for循環和整數。跟蹤和顯示已執行代碼的百分比

要顯示已經處理的for循環的百分比,我使用標誌來指示已經傳遞了多少循環。

MWE可能使更多一點明確:

import time 

N = 100 

flag_15, flag_30, flag_45, flag_60, flag_75, flag_90 = False, False,\ 
False, False, False, False 

for i in range(N): 

    # Large block of code. 
    time.sleep(0.1) 

    if i + 1 >= 0.15 * N and flag_15 is False: 
     print '15%' 
     flag_15 = True 
    elif i + 1 >= 0.3 * N and flag_30 is False: 
     print '30%' 
     flag_30 = True 
    elif i + 1 >= 0.45 * N and flag_45 is False: 
     print '45%' 
     flag_45 = True 
    elif i + 1 >= 0.6 * N and flag_60 is False: 
     print '60%' 
     flag_60 = True 
    elif i + 1 >= 0.75 * N and flag_75 is False: 
     print '75%' 
     flag_75 = True 
    elif i + 1 >= 0.9 * N and flag_90 is False: 
     print '90%' 
     flag_90 = True 
    elif i + 1 == N: 
     print '100%' 

這工作,但相當冗長,真正醜陋。我想知道是否可能有更好/更漂亮的方式來做到這一點。

+0

是重要的標誌?難道你沒有一個變量你增加和設置標誌後? –

回答

1

(發佈第二個答案,因爲該解決方案採用的是完全不同的技術)

當完成百分比達到最低值,你可以創建里程碑值的列表,並打印一條消息。

milestones = [15, 30, 45, 60, 75, 90, 100] 
for i in range(N): 
    #do work here 
    percentage_complete = (100.0 * (i+1)/N) 
    while len(milestones) > 0 and percentage_complete >= milestones[0]: 
     print "{}% complete".format(milestones[0]) 
     #remove that milestone from the list 
     milestones = milestones[1:] 

結果:

15% complete 
30% complete 
45% complete 
60% complete 
75% complete 
90% complete 
100% complete 

與「跨越」的方法我在前面貼了,在這裏你有過這百分比是打印的精確控制。它們不需要均勻分佈,它們不需要被N整除,它們甚至不需要是整數!如果你願意,你可以做milestones = [math.pi, 4.8, 15.16, 23.42, 99]

+0

這個人就像一個魅力,謝謝@Kevin! – Gabriel

3

我喜歡用模數來定期打印狀態信息。

import time 

N = 100 
for i in range(N): 
    #do work here 
    if i % 15 == 0: 
     print "{}% complete".format(int(100 * i/N)) 
print "100% complete" 

結果:

0% complete 
15% complete 
30% complete 
45% complete 
60% complete 
75% complete 
90% complete 
100% complete 

超過100其他的N個值,如果你想打印每15%,你就必須動態地計算步幅,而不是僅僅使用字面值15值。

import time 
import math 

N = 300 
percentage_step = 15 
stride = N * percentage_step/100 
for i in range(N): 
    #do work 
    if i % stride == 0: 
     print "{}% complete".format(int(100 * i/N)) 
+0

這幾乎可行,但它有一些問題。例如,如果我設置了「N = 10」和「percent_step = 25」,它將使用「20%」而不是「25%」的步驟。這可以解決嗎? – Gabriel

+0

不是。由於10不能平均分爲四個部分,所以步幅會稍微降低一點,並且會得到比其他情況更多的狀態信息。 – Kevin

0

你不需要任何標誌。您可以根據i的當前值打印完成。

for i in range(N): 
    # lots of code 

    print '{0}% completed.'.format((i+1)*100.0/N) 
+0

此語法僅適用於python 3+:http:// stackoverflow。com/questions/2456148/python-print-end – RickyA

+0

是;等待lemme轉換成python 2 –

+0

這段代碼讓我無法控制應該顯示的百分比步數。如果'N = 100'這將打印100行,這是不好的。 – Gabriel

2

可以使用寫的組合()的flush()爲漂亮的進度:

import sys 
import time 

for i in range(100): 
    row = "="*i + ">" 
    sys.stdout.write("%s\r%d%%" %(row, i + 1)) 
    sys.stdout.flush() 
    time.sleep(0.1) 

sys.stdout.write("\n") 

進度將顯示如下:

69%====================================================================> 
+0

'flush'似乎不起作用。嘗試用'Sublime'運行這個程序,並獲得100條線,每條線都有一個更大的條。另一方面,它在從終端執行時的廣告中起作用。 – Gabriel