zoukankan      html  css  js  c++  java
  • 效能分析

    此作业的要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2145

    程序地址: https://coding.net/u/lulululu88/p/xiaonengfenxi/git

    一. 预备工作

    将上周没有完成的功能4首先进行完善。

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

    进入文件所在目录,打开控制台,输入:


    ptime wf -s < war_and_peace.txt 
    连续三次运行结果如下:

     

     

    三次平均运行时间为 0.771s

     

     

     2.猜测此程序的瓶颈:

    我推测我的代码中可能出现问题的是正则表达式部分。此处我进行过较多修改,感觉为代码中最犹豫的部分。

    word = Regex.Replace(word, @"[^a-zA-Z0-9u4e00-u9fa5s]", " ");
    
    word = Regex.Replace(word, "[!@#$%^&*()`,./;':"<>`?...]", " ");//过滤替换标点,用空格替换
    
    String[] words = word.Split(' ');

    二.  性能分析

    我使用了vs自带性能分析工具Profiler,主要用来提升性能和寻找瓶颈。

    分析过程如下:

     

     

    分析结果如下:

    函数占用比的分析

     

    调用函数分析:

     

    三.  优化修改

    通过效能分析,我决定修改我的正则表达式部分,代码修改如下:

    更改如下:

    static String[] cut_to_string(string fileword)//利用正则表达式,split分割成一个个单词
    
            {
    
     
    
                string[] filewords = null;
    
                fileword = fileword.ToLower();//全部变为小写字母
    
                char[] ch = { ' ', '!', ',', ':', '.', '"', ';' };
    
                filewords = fileword.Split(ch, StringSplitOptions.RemoveEmptyEntries);
    
     
    
          
    
                return filewords;
    
            }

    优化后时间如下:

     

     

    三次平均时间为0.406s,速度有了很明显的提升,之前平均时间为0.771s,可得这次优化是成功的。

      再次profile,截图如下:

     

  • 相关阅读:
    fastjson 简单使用 及其JSONObject使用
    HttpClient 的使用
    python操作excel xlwt (转)
    matplotlib 设置标题 xy标题等
    matplotlib 饼状图
    Mac下面 matplotlib 中文无法显示解决
    matplotlib 折线图
    matplotlib条形图
    matplotlib直方图
    python matplotlib配置
  • 原文地址:https://www.cnblogs.com/zhaojialu/p/9755927.html
Copyright © 2011-2022 走看看