作业要求 20180925-3 效能分析

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

以war_and_peace作为输入文件连续三次运行消耗时间和CPU参数

CPU参数:Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz 2.30 GHz

第一次运行时间为23.789s

第二次运行时间为24.139s

第三次运行时间为23.822s

猜测程序运行瓶颈:

strcat函数运行耗时:

while(scanf("%s",temp)!=EOF){
        strcat(passage,temp);
        strcat(passage," ");
    }
    
    words = split(passage,&size);

优化方式:改用strcpy:

while (scanf("%s", temp) != EOF) {
        if (strlen(temp) == 0) {
            continue;
        }

        for (i = 0; *(temp + i); i++) {
            if (!(temp[i] <= 'Z'&&temp[i] >= 'A' || temp[i] <= 'z'&&temp[i] >= 'a')) {
                temp[i] = ' ';
            }
        }

        strcpy(words[size++], temp);
    }

优化后运行时间

程序运行时间有了提升,但是和其他同学的差距还是很大。

原文地址:https://www.cnblogs.com/fanhongda/p/9756350.html