Code Reconstruction Practice - One Case Analysis of Code Improvement

zhaozj2021-02-16  65

A few days ago, I took over a project developed using Delphi, where the urgent task is to solve the performance problem of the original code. Among them, there is a variety of code, so special problems, solve the idea and related ideas, for your reference discussion. When designing code for data processing work, we often find that there is a need: display some data to the screen on the screen to make it easy for users to view or other operations. For example, we have the possibility of displaying the area in the TreeView control, then add suppliers subordinate to the corresponding area; or show customers, then join the order number of the customer to the corresponding customer (This is a brief process. There will be various changes in the application, and it is often affected by other business rules - and often these things are confused. In the above project, the original code is designed according to the following ideas: First, query the "region" data into a data object, and add this part data to the tree map by iterative operation; Then, query the "region" "supplier" data separately, then put the part of the data below the "region" node. The following is its main code: adqarea.sql.text: = 'select name from area order by name'; // Query "Region" SQL statement adqarea.open; // Open "Region" record set While not adqarea.eof DO // Traverse the "Region" data becom ... // Write the current record "Region" on the tree map control; adqProvider.sql.text: = 'Select Provider from provider =' select, .Fieldbyname ('name'). Asstring; // query the "supplier" SQL statement that matches the current "region" condition. AdqProvider.open; // Open "Supplier" record set while not adqProvider.eof do begin ... // Add current recorded "vendors" to the "region" node in the tree map control; AdqProvider.next .

This kind of code is similar to what we usually consider, we will naturally think of this. However, it has a very obvious performance defect, in such a code, the number of data queries = "Region" 1. Don't tell, this time when online query, server and network need multiple calculations or transmission work, spend a lot of time resources or bandwidth resources; in addition, data objects need to perform multiple properties to open, Turn off and other operations, there is also a certain extent of resource consumption. Therefore, the result is most obvious, the speed of the screen is definitely not fast, in other words, performance will not meet the speed indicator requirements for practical applications. In-depth analysis of the application, in fact, this is an application that displays customer data in a tree view in a tree view, or puts order data to a tree view from the form of a customer. In a word, it is to process the data arranged in physical order, and a tree will be made into a certain logic order. Everyone knows that because of the cause of the data input order, the location of "vendor" data is arranged in the order of "regional" "dog tooth interleave", as shown in the following table: - -------------------------------------------------- ------------------- Physical location sequential supplier area 1 three China 2 Li Si United States 3 king five China 4 Zheng Six Singapore 5 Li Qi China ...

In our application, it is required to organize the same "region" "supplier" to display and operate in a tree map, and attributing the problem of a data organization. It let me think of the concept of "space" and "time", if it is possible to optimize the defects in the above code, use the speed of the speed of memory storage, reduce the number of queries, can improve the application Performance. From this idea, there are three aspects of the following questions to consider or resolve: First, all the eligible data will be in a while to the data objects in memory; secondly, these data should be organized in the logic order to facilitate The next operation; third, when the data object is traversed, the logical order change is detected by comparing the logical order in the current row and the flag value in the upper row, and controls the parent node and sub-graphics Drawing of nodes. So, I designed the main code (for highlighting theme, Ignore some data detection and error handling code): Set a variable previousareaname to record the "region" name of the data before the current data; AdqProvider. SQL.Text: = 'SELECT Provider, Area.name from provider Right Jion area ON AREA.NAME'; // Query all regional and included vendor data. AdqRovider.open; previousareaname: = '; while not adqProvider.eof do begin if previousareaname <> adqProvider.fieldByname (' name '). asstring the begin ... // Add current record "Region" to the node of the tree Go as a new parent node previousAreaname = adqProvider.fieldByname ('name'). Asstring; end; if not adqProvider.fieldbyName ('provider'). Isnull dam // Due to Right Jion, "Supplier" in some areas The value may be null; ... // Add the current record "supplier" to the current "region" parent node in the tree map control; adqProvider.next; end; these code has achieved three aspects described above The requirements are pleased that the display speed of this part of the function program has an order of magnitude, which is raised from the original 11 seconds to 1 second. Therefore, by in-depth analysis of the program process, the process of processing the program is redesigned, and we have achieved optimization performance. In summary, the key is that the new program can load the required data to operate in the local memory, which greatly reduces the slower network transmission speed. Further, this solution is similar to the data processing mode of "preloaded data", so that data is loaded from a slower medium in a relaxed medium to faster memory, which is operated to improve the response speed. purpose.

This method is generally existing in various data application environments. When the Windows system is started, the system icon library with the registry data is loaded into the memory, so it can make the application can get in a very short time. System icon or registry data. Promoting, in the programming in data processing, we can learn from this batch to acquire data. Of course, not all situations are suitable for use "bulk loading data" processing method, if the amount of data is particularly large, we still need to divide the data into several small parts, as for how to deal with The actual situation is fixed. These are the results of current reconstruction work, code improvement, mainly from the perspective of optimization performance; in the future, we can continue to work some improvements from the perspective of OOP, interface and other programming. There is a good job lies in a "diligent" word. It is necessary to design a good performance of the code, and must work in research and improve the universal code, and constantly accumulate good experience. This article makes a small attempt in this regard, throwing bricks, and expects you to talk about yourself some good design experience or experience, and promote common improvement. QQ: 272568028E-mail: gjguoji@tom.com welcomes friends to contact, in-depth research and discussion.

转载请注明原文地址:https://www.9cbs.com/read-24149.html

New Post(0)