work3

  学号:2017*****7219;
  姓名:邰嘉琛;
  码云项目仓库:https://gitee.com/tjc666/word_frequency/tree/master

代码;

from string import punctuation
def process_file(dst):3
try:
f = open(dst)
except IOError, s:
print s
return None
try:
bvffer = f.read()
except:
print "Read File Error!"
return None
f.close()
return bvffer

def process_buffer(bvffer):
if bvffer:
word_freq = {}
for item in bvffer.strip().split():
word = item.strip(punctuation+' ')
if word in word_freq.keys():
word_freq[word] += 1
else:
word_freq[word] = 1
return word_freq

def output_result(word_freq):
if word_freq:
sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
for item in sorted_word_freq[:10]:
print item

if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('dst')
args = parser.parse_args()
dst = args.dst
bvffer = process_file(dst)
word_freq = process_buffer(bvffer)
output_result(word_freq)

 

 

在命令中输入python word_freq.py Gone_with_the_wind.txt运行代码

work3_第1张图片

使用cProfile进行性能分析
python -m cProfile word_freq.py Gone_with_the_wind.txt

work3_第2张图片

修改代码if word in word_freq.keys()修改为if word in word_freq 再次分析work3_第3张图片

性能分析的意义:找到代码的性能低的原因,减少内存的使用效率更高,同样的处理获得更快的时间、

 

你可能感兴趣的:(work3)