第三次作业-功能测试

因为文件的编码问题,所以出现了小问题单词全部都输出了,这是三次的程序执行图

猜测程序瓶颈是读入文件,读入文件是整个程序循环次数最多的地

file_name = input("")#在命令行输出
path = "C:\Users\Administrator\Desktop\" +file_name +'.txt'
if not os.path.exists(path):
    print('请把目标文件放在桌面')
else:
    with open(path,'r',encoding='utf-8') as f:
        for line in f:
            lines_count = lines_count + 1
            chars_count = chars_count + len(line)
            match = re.findall(r'[^a-zA-Z0-9]+', line)  # 只要英文单词,删掉其他字符
            for i in match:
                line = line.replace(i, ' ')
                lines_list = line.split()  # 对单词进行统计
            for i in lines_list:
                if i not in words_dict:
                    words_dict[i] = 1
                else:
                    words_dict[i] = words_dict[i] + 1

性能分析我使用了pycharm自带的profile分析工具: 

因为本身的能力不足,以及时间的短暂,没办法解决,直接分析wf这个程序,所以选择的是分析其中wd这个程序,从图中我们可以看到程序最大最大的瓶颈是input板块。

 至于代码的优化,因为对python的理解还不够深入,暂时没办法实现。

原文地址:https://www.cnblogs.com/cherishcherry/p/7597882.html