• 梁梦瑶 20190919-3 效能分析


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

    git地址https://e.coding.net/hahaa/wf.git

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

    第一次运行时间

     第二次运行时间

     第三次运行时间

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

    要求 给出代码片断,并说明为什么你会认为此处是瓶颈,预计优化会有达到多好的效果
    1.第一处我猜测获取文件中的单词个数的时候,由于上一周我的正则表达式出现了一点错误,导致统计字数不对,这周我修改了,修改方法是:先将所有单词转换为小写,然后再筛选。
    但我认为此处还可以优化,原因是将大写字母转换为小写字母在进行筛选会很浪费时间。预计优化后运行时间会快那么一点点。

    2.第二处是功能四的函数,为了输出格式问题,我又将单词统计的方法重新书写了一遍,如果考虑优化的话,这个地方还可以修改,可直接运用单词统计的封装函数
    def redirect(txt): #功能四
        words = re.findall(r'[a-z0-9^-]+', txt.lower())
        user_counters=Counter(words)
        total=0
        for user_counter in user_counters:
            total+=1
        print("total %d words
    "%total)
        lsts=user_counters.most_common(10)
        for lst in lsts:
            print("%s  %d"%(lst[0],lst[1]))
    
    

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

    要求 分析为什么此处是瓶颈。

    要求 profile需要得到函数的运行时间和次数。仅得到CPU和内存占用,不得分。

     

     代码片断:

    def redirect(txt): #功能四
        words = re.findall(r'[a-z0-9^-]+', txt.lower())
        user_counters=Counter(words)
        total=0
        for user_counter in user_counters:
            total+=1
        print("total %d words
    "%total)
        lsts=user_counters.most_common(10)
        for lst in lsts:
            print("%s  %d"%(lst[0],lst[1]))

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

    1.优化前:

    def redirect(txt): #功能四
        words = re.findall(r'[a-z0-9^-]+', txt.lower())
        user_counters=Counter(words)
        total=0
        for user_counter in user_counters:
            total+=1
        print("total %d words
    "%total)
        lsts=user_counters.most_common(10)
        for lst in lsts:
            print("%s  %d"%(lst[0],lst[1]))

    优化后:
    将findall()函数进行了优化,函数进行封装。

    def redirect(txt): #功能四
        words = re.findall(r'[a-zA-Z0-9^-]+', txt)
        coTotal(words)

    2.优化前

    def file_name(path): 
        path=path+'.txt'
        try:
            with open(path,encoding='utf-8') as f: 
                content=f.read()
        except FileNotFoundError: #异常处理,找不到文件,输出文件不存在
            msg="The file"+path+"does not exist."
            print(msg)
        else:
            words=re.findall(r'[a-z0-9^-]+',content.lower())
            coTotal(words)

    优化后

    def file_name(path): #功能二实现,输入不带后缀的文件名
        path=path+'.txt'
        with open(path,encoding='utf-8') as f: 
            content=f.read()
            words=re.findall(r'[a-z0-9^-]+',content.lower())
            coTotal(words)

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

    三个函数此时的花费:

     再次测试三次:

     

     

  • 相关阅读:
    谷歌浏览器中安装JsonView扩展程序
    谷歌浏览器中安装Axure扩展程序
    PreferencesUtils【SharedPreferences操作工具类】
    Eclipse打包出错——提示GC overhead limit exceeded
    IntentActionUtil【Intent的常见作用的工具类】
    DeviceUuidFactory【获取设备唯一标识码的UUID(加密)】【需要运行时权限的处理的配合】
    AndroidStudio意外崩溃,电脑重启,导致重启打开Androidstudio后所有的import都出错
    DateTimeHelper【日期类型与字符串互转以及日期对比相关操作】
    ACache【轻量级的开源缓存框架】
    WebUtils【MD5加密(基于MessageDigest)】
  • 原文地址:https://www.cnblogs.com/summerkingy/p/11568471.html
Copyright © 2020-2023  润新知