[英]Python - how to tell if a process is i/o bound?
我有一個解析100MB
文件的程序,然后對數據應用了一些功能。 我沒有實現檢查瓶頸的功能...
所以我只是在注釋中添加實現,然后pass
為什么python使用那么多內存?
解析文件需要15分鍾,我可以看到python使用的內存為3GB,CPU的使用率為15%,內存的使用率為70%。
是否應用了io綁定程序?
如何固定解析? 還是與慢速解析無關?
文件樣本:年齡和工資
50 1000
40 123
1233 123213
碼:
def parse(pathToFile):
myList = []
with open(pathToFile) as f:
for line in f:
s = line.split()
age, salary = [int(v) for v in s]
Jemand = Mensch(age, salary)
myList.append(Jemand)
return myList
您的代碼可以大大提高速度:
with open(pathToFile) as f:
for line in f:
s = line.split()
age, salary = [int(v) for v in s]
Jemand = Mensch(age, salary)
myList.append(Jemand)
之所以慢是因為
append
它可能會成為准一線客:
with open(pathToFile) as f:
myList = [Mensch(*(int(x) for x in line.split())) for line in f]
(使用列表鏈表理解和生成器理解,只要使用*
解包將參數傳遞給類即可)
您觀察到的性能不佳可能是由於Python垃圾收集器中的錯誤引起的。 要解決此問題,請在構建列表時禁用垃圾收集,並在完成后將其打開。 有關更多詳細信息,請參見此SO文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.