Csapp Reading Notes (7)-program execution time calculation

Source: Internet
Author: User
Tags integer numbers

This chapter discusses how to calculate Program The execution time of each part. The measurement parameters are divided into two methods: one is time-based calculation, and the other is the number of clock periods. The former generally uses low-frequency scheduled interruptions for acquisition or update, and the latter uses counters, with each clock cycle increasing by 1. Currently, computer systems generally adopt multi-task and multi-user mode, but there is only one internal CPU (regardless of multi-core and multi-CPU ). The system creates an environment for users to simulate multi-task running at the same time. For a seemingly concurrent task, it is called concurrency. However, since there is only one part of the CPU, all the tasks are actually completed in serial mode, but the system schedules all the tasks and does it later, make the tasks appear to be processed in parallel. In addition, the overhead caused by Task Switching (such as the execution of the stored field and other kernel space) should also be taken into account. Therefore, to measure the processing or execution time of functions in a process, it is necessary to consider the existence of such errors. First, let's look at the time measurement method. Since low frequency Clock interruption is used, its accuracy is related to the frequency of interruption. If the frequency is too high, the error caused by the overhead is large. If the frequency is too low, the measurement accuracy is reduced. Therefore, you need to select a better value. This chapter uses the 10 ms interrupt interval. In addition, based on the experimental results, when the time difference of measurement is greater than ms, the error mean can be ensured to be less 4% . Let's look at the time period counting method. This method is based on the existence of a register for the number of clock cycles since the system was started. In the ia32 architecture, this counter uses 64-bit unsigned integer numbers. If the machine clock speed is 1 GHz, it can support counting around 570. This calculation method has nothing to do with the performance of the machine, because it does not directly calculate the time, but instead of the number of clock cycles. Let's take a look at the impact of various situations: (1) Context switching: Context switching has a great impact, especially when the system load is heavy, the error will increase. Measures can be taken to minimize the time interval to be measured to avoid context switching. (2) high-speed cache: due to the difference in the high-speed cache and memory access rate, coupled with different replacements Algorithm . Although the influence of this factor is not as big as that of the former, it is unacceptable. Measures can be taken: the function to be tested can be called once in advance before the actual calculation to play the role of warm-up caching. If you do not want the cache for data access, you can first use the warm-up function and then clear the data cache. In this way, onlyCodeCached, and data still needs to be accessed from memory. Next let's take a look at the K-best method based on this measurement. In this way, for test data, you can select K data with the fastest measurement time value. Of course, the K data must meet the error that does not exceed the given upper limit D, and a maximum m of measurement data is given. The convergence of the measurement method is defined as that K data has been selected and the error D is satisfied, and the total number of measurements does not exceed M. This method is only applicable when the time interval is less than 7 ms. . In conclusion, the interruption timing method is good enough when the calculation time is greater than 1 s. The calculation time is between 10 ms and 10 ms ~ For 1 s, the K-best method is used for light loading. If the calculation time is less than 10 ms, the K-best method can achieve better results.

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.