Use Performance Analysis to improve code efficiency -- By Yishi

Source: Internet
Author: User

 

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.

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.