1.个人信息
2.程序分析
1首先定义def process_file函数,将文件读到缓冲区并关闭,用open()打开文件、read()读取文件、close()关闭文件
def process_file(dst): # 读文件到缓冲区 try: # 打开文件 f1 = open(dst, "r") except IOError as s: print (s) return None try: # 读文件到缓冲区 bvffer = f1.read() except: print ("Read File Error!") return None f1.close() return bvffer
2将读取出的文件放入缓冲区中,对数据进行操作,切割字符串中的符号分开单词,统计每个单词的频率,存放在字典word_freq
def process_buffer(bvffer):
if bvffer:
word_freq = {}
# 下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq
bvffer=bvffer.lower()
for x in '~!@#$%^&*()_+/*-+\][': bvffer=bvffer.replace(x, " ") words=bvffer.strip().split() for word in words: word_freq[word]=word_freq.get(word,0)+1 return word_freq
3遍历切割完的字符串,并输出统计频率Top 10 的单词
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]: # 输出 Top 10 的单词 print(item)
4封装main函数,
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)
3.性能分析结果及改进
-
执行次数最多的代码为:
word_ = i.strip(punctuation + " ") -
执行时间最长的代码为:
word_freq = process_buffer(bvffer)
4.程序运行命令、运行结果截图及改进后的程序运行命令结果截图
5.对此次任务的总结与反思
通过本次的学习,使我对程序的性能分析及git分支语句的使用有所了解,但我对他还是有所困惑,我还要在课下对其进行研究一下。