20180925-3 Performance Analysis

Source: Internet
Author: User

First, draw the program run time

Run as follows:

First run time is 0.942 s

Second run time is 0.826 s

The third run time is 0.861 s

Average run time: 0.876 s

CPU parameters: Intel (R) Core (TM) i5-4200m CPU @ 2.50GHz 2.50GHz

Second, guessing the bottleneck of the program

Guess one: Each query map container whether the word has been more time-consuming, because the word must be queried once, the optimization may run 5% faster.

    if (mp[str]==0) {        s.push_back (str);        MP[STR]+ +;        Totalword+ +;    } Else {        Mp[str]+ +;    }

Guess two: Judging whether the composition of the word is time-consuming, because to judge each character, the optimization may run 10% faster.

if (Isalpha (ch)! =0| | (ch=='\ '&&len>0) | | ch=='-'| | (ch>='0'&&ch<='9'&&len >0))

Third, use profile to identify bottlenecks

CPU Usage:

number of function calls and run time:

Here you can see that the most time-consuming three functions are: Map lookup comparison function, string concatenation function and judge whether it is a letter function.

Map Lookup comparison function and string concatenation function:

if 0 ) {    s.push_back (str);    MP[STR]+ +;    Totalword+ +;} Else {    Mp[str]+ +;}

To determine the letter function:

if 0 ' \ ' '&&len>0'-'0'  9'&&len>0))

Analysis: The two functions of map and string may need to call the stack, consuming more time, and judging the letter function because of the number of executions, resulting in more than the last run time.

Iv. optimization Procedures

Map and string can't figure out how to optimize, judging the function to compare directly with the letter. The corresponding function changes as follows:

if(text[i]>='a'&&text[i]<='Z') {        ...}Else if(text[i]>='A'&&text[i]<='Z') {    ...}Else if((text[i]==' '&&len>0)|| text[i]=='-') {        ...}

Five, again profile test

Ptime Run Time:

First run time is 0.826 s

Second run time is 0.817 s

The third run time is 0.827 s

Average run time: 0.823 s

0.053s faster than unmodified

CPU Usage:

Number of function calls and run time:

You can see that the scale of the map and string functions is basically unchanged, judging that the letter function cannot be found, and that the running time percentage of the other running functions is less than 0.1%.

Vi. Summary

After modification, the operation speed of the program has a certain increase, there are many places I have not been able to modify, if can be further modified, the operation speed of the program will be further improved. In the future procedures, we must be flexible use of various tools, optimize the program, improve the speed of the program.

20180925-3 Performance Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.