When I was working on my project in the ASE class, the instructor recommended that we use the ANALYZE function of Visual Studio to find and correct the sections that make code efficiency lower. The following describes how to use this function.
Non-CS professionals like me tend to focus only on implementing functions when writing code, and do not pay special attention to code efficiency when writing. For example, the example below is a program that processes txt files in batches, extracts English words with a length greater than 1, and counts the frequency. At the beginning, I just wanted to traverse the characters in the file one by one, think of the strings between two separators as a word, and add them to the word tree. After writing, I think most of the running time of the program should be spent in the process of adding words to the tree, because each letter of the word needs to be compared. So I want to use ANALYZE for verification. It's easy to use. On the VS menu bar, click ANALYZE and then click start performance analysis. Generally, the sampling method is used, so no more settings are required, the result is as follows:
The results of ANALYZE are displayed in multiple views, which is not very convenient. You can click the drop-down menu on the Right of Current View and select Function Details View, you can easily see the portion of the program that occupies a large amount of running time. VS can even precisely locate the positions of these program segments, such:
So I found that scanning txt files one by one using the fscanf function repeatedly, this function is very time-consuming to run, so I want to read all the characters in the file into a string at a time, then, you can directly operate on the string, instead of using the function of the fscanf operation file every time. Use ANALYZE for verification after modification. The following results are obtained:
As I thought before, the program spends some time adding words to the AddRecord function of the word tree, which indicates that the above changes are correct.