第三次作业-效能分析

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

运行截图:

连续三次运行截图:

第一次运行:

消耗时间:4.3s     CPU<20%

第二次运行:

消耗时间:5s      CPU<20%

第三次运行:

消耗时间:4.5s      CPU<20%

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

foreach (string key in arrayList)
            {
                //keyArray[index] = key;
                keyArray[index] = Convert.ToString(key);
                valueArray[index] = Convert.ToInt32(hashtable[key]);
                index++;
            }

此处使用了排序递归算法,可以使程序快速的进行下去。

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

函数1

 static void Main(string[] args)
        {
            bool flag = true;
            while (flag)
            {
                Console.WriteLine();
                Console.WriteLine("效能分析输入1");
                string choice = Console.ReadLine();
                switch (choice)
                {
              
                    case "1":                   
                        string[] files = Directory.GetFiles("..\..\essay\");
                        foreach (string f in files)
                        {
                            Console.WriteLine(f);
                            FileUtil file3 = new FileUtil();
                            file3.CountEachWord(f, 3);
                        }
                        break;
                   
                }

            }
        }

函数2

public void CountEachWord(string url, int choice)
        {
            int count = 0; 
            StreamReader streamReader = new StreamReader(url);
            string line;
            Regex regex = new Regex(@"[A-Za-z]+[A-Za-z0-9]*");
            while ((line = streamReader.ReadLine()) != null)
            {

                MatchCollection matchCollection = regex.Matches(line);
                foreach (Match word in matchCollection)
                {
                    string words = word.ToString();
                    if (hashtable.Contains(words))
                    {
                        int j = Convert.ToInt32(hashtable[words]) + 1;
                        hashtable[words] = j;
                    }
                    else
                    {
                        hashtable.Add(words, 1);
                    }
                }
            }
            count = hashtable.Keys.Count;
            Console.WriteLine("total: " + count);
            Console.WriteLine();
            streamReader.Close();
            SortHashtable(hashtable, choice);

        }

函数3

 private void QuickSort(int[] valueArray, string[] keyArray, int v1, int v2)
        {
            if (v1 < v2)
            {
                int index2 = Division(valueArray, keyArray, v1, v2);
                QuickSort(valueArray, keyArray, index2 + 1, v2);
                QuickSort(valueArray, keyArray, v1, index2 - 1);
            }
        }

函数大多调用了C#中定义好的函数,所以运行起来会使程序变慢。

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

git地址: https://git.coding.net/ZhangEJ/wf.git

原文地址:https://www.cnblogs.com/zej87/p/7596295.html