20190919-3 Effectiveness Analysis

See operational requirements [https://edu.cnblogs.com/campus/nenu/2019fall/homework/7628]

 

• Requirements 0 to war and peace as the input file, and re-read by the file system to read into. Three consecutive runs, given time, CPU consumption of each parameter.

Method of operation

ptime wf -s < war_and_peace.txt

First run:

Second run:

Third run:

CPU parameters: Intel (R) Core (TM) i3-8145U CPU @ 2.10GHz 2.30 GHz

The third running time: 4.164s

The third running time: 5.202s

The third running time: 4.167s

Average running time: 4.511s

 

•  Requirements 1 shows the bottleneck in your guessing program.

4 when performing frequency statistics function, firstly read one by one to the characters in the document, then in accordance with spaces and special symbols, characters one by one will be cut into the word, and find the word if the press count of the rules present in the array of structures, the last an array of structures according to the word order of frequency. From this analysis, the program mainly to find words and word ordering both time-consuming. In terms of sequencing, using the C language compiler library comes with quick-sort function qsort, so in terms of optimization, I can not guarantee to do better. Then find the word module, I became the focus of speculation object.

//顺序查找法
for (t = 0; t < count; t++)
{
    if (strcmp((words + t)->words, temp) == 0)
    {
        (words + t)->frequent++;
        break;
    }
}
if (t == count)
{
    (words + count)->frequent = 1;
    strcpy((words + count)->words, temp);
    count++;
}

 

• 要求2 通过 profile 找出程序的瓶颈。

 

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

由于本人查找顺序表用的算法是顺序查找,因此先想到用折半查找法,但考虑到文本读取的时候,记录单词的结构体数组基本无序,因此按照理论来讲,折半查找也不会明显改善查找效率。即便是这样,在没有更好的方案的前提下,个人还是尝试用折半查找去改善查找效率。

int low, high, mid;
low = 0;
high = count;
if (count == 0)
{
    (words + count)->frequent = 1;
    count++;
}
while (low <= high)
{
    mid = (low + high) / 2;
    if (strcmp((words + mid)->words, temp) == 0)
    {
        (words + t)->frequent++;
        break;
    }
    if (strcmp((words + mid)->words, temp) > 0)
        high = mid - 1;
    if (strcmp((words + mid)->words, temp) < 0)
        low = mid + 1;
}
if (low > high)
{
    (words + count)->frequent = 1;
    count++;
}

 

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

 

• 要求5 程序运行时间。

附上本人代码wf.exe程序git地址,供老师测试[https://e.coding.net/secret/ASETest1.git]

 

Guess you like

Origin www.cnblogs.com/liuxp775/p/11564875.html