我已經在下面創建腳本處理基於用戶輸入的兩個文件並生成第三個結果文件。基於python文件的程序需要時間來執行
腳本正常執行沒有任何問題,但是當兩個文件都有很高的數量,那麼它需要時間。在測試過程中,我測試了InputFile-1有500000條記錄,InputFile-2有100條記錄。
所以想檢查是否有任何優化的可能性減少總體執行時間。請分享您的想法。
在此先感謝。
import ipaddress
filePathName1 = raw_input('InputFile-1 : ')
filePathName2 = raw_input('InputFile-2: ')
ipLookupResultFileName = filePathName1 + ' - ResultFile.txt'
ipLookupResultFile = open(ipLookupResultFileName,'w+')
with open(filePathName1,'r') as ipFile:
with open(filePathName2,'r') as ipCidrRangeFile:
for everyIP in ipFile:
ipLookupFlag = 'NONE'
ipCidrRangeFile.seek(0)
for everyIpCidrRange in ipCidrRangeFile:
if (ipaddress.IPv4Address(unicode(everyIP.rstrip('\n'))) in ipaddress.ip_network(unicode(everyIpCidrRange.rstrip('\n')))) == True:
ipLookupFlag = 'True'
break
if ipLookupFlag == 'True':
ipLookupResultFile.write(everyIP.rstrip('\n') + ' - Valid_Operator_IP' + '\n')
else:
ipLookupResultFile.write(everyIP.rstrip('\n') + ' - Not_Valid_Operator_IP' + '\n')
ipFile.close()
ipCidrRangeFile.close()
ipLookupResultFile.close()
記錄樣本爲INPUTFILE-1: 192.169.0.1 192.169.0.6 192.169.0.7
記錄樣本爲INPUTFILE-2:ResultFile.txt
192.169.0.1/32
192.169.0.6/16
255.255.255.0/32
255.255.255.0/16
192.169.0.7/32
記錄樣本:
192.169.0.1 - Not_Valid_Operator_IP
192.169.0.6 - Valid_Operator_IP
192.169.0.7 - Not_Valid_Operator_IP
退房http://stackoverflow.com/questions/582336/how-can-you-profile-a-script/582337#582337 –
你可以編輯問題給小樣本文件,並預期'ResultFile。 txt'輸出? –
@MartinEvans - 編輯的文件記錄有問題...... – AJNEO999