付宇泽20190919-3 效能分析

此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/7628

git地址https://fyzs.coding.net/p/Analysis/git

要求0 以 战争与和平 作为输入文件,重读向由文件系统读入。连续三次运行,给出每次消耗时间、CPU参数。 (2分)

           cpu参数:Intel Core i7-9750H CPU  @2.60Hz 2.59GHz

           消耗时间:0.712s,0.592s,0.654s

           

           

           

  要求1 给出你猜测程序的瓶颈。你认为优化会有最佳效果,或者在上周在此处做过优化 (或考虑到优化,因此更差的代码没有写出)          

def count_split(str):
    text = re.findall(r'[a-z0-9^-]+', str)  
    dict = {}                                    #创建字典
    for str in text:                             #遍历文件内单词
        if str in dict.keys():
           dict[str] = dict[str] + 1
        else:
           dict[str] = 1
    word_list=sorted(dict.items(), key=lambda x: x[1], reverse=True)
    return word_list

此处为优化后代码,上版本使用replace进行特殊字符的替换。

                redirect_word = sys.stdin.read().lower()
                count_list = count_split(redirect_word)
                print('total', len(count_list))
                for i in range(10):
                    print('{:20s}{:>5d}'.format(count_list[i][0], count_list[i][1]))

此处为上次作业后完善的功能4,应该还有优化空间。

要求2 通过 profile 找出程序的瓶颈。给出程序运行中最花费时间的3个函数(或代码片断)。要求包括截图。 (5分)

 

程序最花费时间的三个函数:count_split(),findall(),read()

此三处为几个核心功能都需要调用的代码

要求3 根据瓶颈,"尽力而为"地优化程序性能。 (5分)

def count_split(str):
    p = re.compile('[-,$()#+&*.![:/"?—]')               #指定特殊字符
    text = re.sub(p, " ", str).split()
    dict = {}                                    #创建字典
    for str in text:                             #遍历文件内单词
        if str in dict.keys():
           dict[str] = dict[str] + 1
        else:
           dict[str] = 1
    word_list=sorted(dict.items(), key=lambda x: x[1], reverse=True)
    return word_list

findall()函数花费较多时间,使用compile函数和sub函数替换完成功能。findall()函数找出所有英文单词,compile()指定字符后用sub()替换为空格。

要求4 再次 profile,给出在 要求1 中的最花费时间的3个函数此时的花费。要求包括截图。(2分)

count_split()中原本花费时间较多的findall()函数,使用compile()函数和sub()替换完成功能.减少了消耗时间。

要求5 程序运行时间。根据在教师的机器 (Windows8.1) 上运行的速度排名,分为3档。此题得分,第1档20分, 第2档10分,第3档5分。功能测试不能通过的,0分。(20分)

 

转载于:https://www.cnblogs.com/Fuyuze/p/11579634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值