次のコードを検討してください。
ここで、6列(0〜5)の入力ファイルを読み取ります
- 変数 historyends を 5000 に初期化します。
- 次に、column0 の値 i,e job[0] < 5000 の場合、入力ファイルの 5000 行をリスト (historyjobs) に追加します。それ以外の場合は、残りの行を別のリスト (targetjobs) の eof まで追加します。
- 次に、すべての historyjobs リスト item3、item4、item5 のすべてのコンテンツが targetjobs の最初のリスト item3、item4、item5 と等しい場合、この条件が満たされると、それらの historyjobs all item1 がリスト listsub に追加されます。
- 次に、listsub 内のアイテムの移動平均を見つけ、リストを反転し、リスト a に格納します。listsub 内のアイテム > a*0.9 の条件を満たしている場合は、条件を満たし、結果のアイテムをリスト condsub に格納します。
- 次に、inputfile を再度開き、column0 が condsub の項目と等しいかどうかを確認し、満たす場合は column1 をリスト condrun に追加します。
- 最後に出力ファイルを開き、colum0 に targetjobs i,ej の最初のリストの 2 番目の項目を書き込みます。リスト condrun の最小項目、column5 はリスト condrun の長さ、最後の 4 列は条件に基づいています。
最後に、変数 historyends を次の項目 int(targetjobs[1][0]) に割り当てることにより、while ループを使用して手順全体を繰り返しています。
from __future__ import division import itertools history_begins = 1; history_ends = 5000; n = 0; total = 0 historyjobs = []; targetjobs = [] listsub = []; listrun = []; listavg = [] ; F = [] ; condsub = [] ;condrun = [] ;mlistsub = []; a = [] def check(inputfile): f = open(inputfile,'r') #reads the inputfile lines = f.readlines() for line in lines: job = line.split() if( int(job[0]) < history_ends ): #if the column0 is less then history_ends(i,e 5000 initially) historyjobs.append(job) #historyjobs list contains all the lines from the list whose column1 < history_ends else: targetjobs.append(job) #historyjobs list contains all the lines from the list whose column1 > history_ends k = 0 for i, element in enumerate(historyjobs): if( (int(historyjobs[i][3]) == int(targetjobs[k][3])) and (int(historyjobs[i][4]) == int(targetjobs[k][4])) and (int(historyjobs[i][5]) == int(targetjobs[k][5])) ): #historyjobs list all contents in column3,column4,column5 is equal to targetjobs first list column3,column4,column5 listsub.append(historyjobs[i][1]) #when if condition true add those historyjobs column1 to list listsub def runningMean(iterable): """A generator, yielding a cumulative average of its input.""" num = 0 denom = 0 for x in iterable: num += x denom += 1 yield num / denom def newfun(results): results.reverse() # put them back in regular order for value, average in results: a.append(value) return a #to return the value def runcheck(subseq): f = open('newfileinput','r') #again read the same inputfile lines = f.readlines() for line in lines: job = line.split() for i, element in enumerate(subseq): if(int(job[1]) == int(subseq[i])): # if the column1 value of the inputfile becomes equal to list obtained condrun.append(str(job[2])) #return the value of column2 which satisfies the if condition return condrun def listcreate(condrun,condsub): f1 = open('outputfile','a') #outputfile to append the result s = map(int,condrun) j = int(targetjobs[0][2]) targetsub = int(targetjobs[0][1]) if(condsub != []): try: convertsub = int(condsub[-1]) a=sum(s)/len(s) c=max(s) d=min(s) e1=abs(j-a) er1=e1/j g=len(s) h=abs(convertsub-targetsub) f1.write(str(j)) f1.write('\t') f1.write('\t') f1.write(str(round(a,2))) f1.write('\t') f1.write('\t') f1.write(str(round(er1,3))) f1.write('\t') f1.write('\t') f1.write(str(c)) f1.write('\t') f1.write('\t') f1.write(str(d)) f1.write('\t') f1.write('\t') f1.write(str(g)) f1.write('\t') f1.write('\t') f1.write(str(h)) f1.write('\t') f1.write("\t") if (float(er1) < 0.20): f1.write("good") f1.write("\t") else : f1.write("bad") f1.write("\t") if (float(er1) < 0.30): f1.write("good") f1.write("\t") else : f1.write("bad") f1.write("\t") if (float(er1) < 0.40): f1.write("good") f1.write("\t") else : f1.write("bad") f1.write("\t") if (float(er1) < 0.50): f1.write("good") f1.write("\n") else : f1.write("bad") f1.write("\n") except ZeroDivisionError : print 'dem 0' else: print '0' f1.close() def new(): global history_ends while 1: #To repeat the process untill the EOF(end of input file) check('newfileinput') #First function call if(len(targetjobs) != 1): history_ends = int(targetjobs[1][0]) #initialize historyends to targetjobs second lines first item mlistsub = map(int,listsub) results = list(itertools.takewhile(lambda x: x[0] > 0.9 * x[1], itertools.izip(reversed(mlistsub), runningMean(reversed(mlistsub)))))#call runningmean function & check the condition condsub = newfun(results) #function to reverse back the result condrun=runcheck(condsub) #functionto match & return the value listcreate(condrun,condsub) #function to write result to output file del condrun[0:len(condrun)]#to delete the values in list del condsub[0:len(condsub)]#to delete the values in list del listsub[0:len(listsub)]#to delete the values in list del targetjobs[0:len(targetjobs)]#to delete the values in list del historyjobs[0:len(historyjobs)]#to delete the values in list else: break def main(): new() if __name__ == '__main__': main()
サンプル入力ファイル (ファイル全体に 200,000 行が含まれます):
1 0 9227 1152 34 2
2 111 7622 1120 34 2
3 68486 710 1024 14 2
6 265065 3389 800 22 2
7 393152 48438 64 132 3
8 412251 46744 64 132 3
9 430593 50866 256 95 4
10 430730 10770 256 95 4
11 433750 12701 256 14 3
12 437926 2794 64 34 2
13 440070 43 32 96 3
13 440070 43 32 96 3
14 440102 44 32 96 3
15 440357 43 32 96 3
16 440545 43 32 96 3
17 440599 43 32 96 3
18 440625 43 32 96 3
19 440999 84 32 96 0
20 441574 44 32 96 3
21 442667 7914 512 14 3
22 443249 45 32 96 3
25 443797 3260 128 68 4
26 443799 3746 128 68 4
27 445357 31 8 29 3
28 445393 31 8 29 3
29 445445 28 8 29 3
30 445488 29 8 29 3
サンプル出力ファイルの内容:
930 1389.14 0.494 3625 977 7 15 bad bad bad good
4348 1331.75 0.694 3625 930 8 164 bad bad bad bad
18047 32237.0 0.786 61465 17285 3 325774 bad bad bad bad
1607 1509.0 0.061 1509 1509 1 6508 good good good good
304 40.06 0.868 80 32 35 53472 bad bad bad bad
7246 7247.0 0.0 7247 7247 1 9691 good good good good
95 1558.0 15.4 1607 1509 2 2148 bad bad bad bad
55 54.33 0.012 56 53 3 448142 good good good good
31 76.38 1.464 392 35 13 237152 bad bad bad bad
207 55.0 0.734 55 55 1 370 bad bad bad bad
誰かがコードをより速く実行するためのいくつかの変更を提案できれば、それは役に立ちます...
入力ファイル:
column 0-->represents jobnum
column 1-->represents submittime
column 2-->represents runtime
column 3-->represents userid
column 4-->represents numberof processor
column 5-->represents queueid
ある種の履歴が5001行目を読み取り、そのcol3、col4、col5の値がその5000行の値と等しいかどうかを比較するため、入力ファイルの最初の5000行を維持します.20行に一致する値がある場合、条件col2を満たすすべての行リストに。このリストの実行中の平均を見つけて、結果を別のリストに保存します1.すべてのアイテムの条件をチェックします (リスト1 > リスト*0.9) 条件を満たすアイテムは、別のリストに追加します list3. 履歴の 5000 行の col0 に一致する list3 のすべての項目は、col2 を list4 に格納します。最終的な list4 の平均を書き込むためにファイルを開く必要があります。EOF まで履歴を次の行にインクリメントして、同じことを繰り返します。