One problem with the programming

xiaoxiao2021-03-06  49

In the programming, there is often a problem that the program execution efficiency is low, and sometimes the problem is in the circulation body. We look at the problems you should pay attention to in the program by one of the following examples.

The code main function in this example is that the timing checks if several tables in the database have been updated, and if there is an update, the corresponding data grid in the display window is automatically refreshed. Check if the data of these tables is updated by determining whether a flag field value of the corresponding table name in a data table is 1, if it is 1, then the table has been updated, the data grid needs to be refreshed.

The following code is the code before the optimization (the code runs on the Timer Control Ontime event):

Try

/ / Stop timing of the Timer control

TimeUpdate.enabled: = false;

Read the data of the table that records the refresh state

//AdoQryReadupdate.refresh;

//AdoQryReadupdate.Requery ();

AdoQryReadupdate.Close;

AdoQryReadupdate.Open;

AdoQryReadupdate.first;

// Judgment the table has been updated by looping

With adoqryreadupdate do

While not Eof do

Begin

Bflag: = false;

/ / Judgment is the flag field of the corresponding table name is 1, if 1 is refreshed the corresponding data grid

IF (FLAG '). Asinteger = 1)

And (FieldByName). Asinteger = loginchannelid) THEN

Begin

Sname: = FieldByname ('name'). asstring;

// Refresh the data grid code, here is slightly

......

IF bflag dam

Begin

// Modify the corresponding table flag value of 0

EDIT;

FieldByname ('flag'). Asboolean: = false;

POST;

END;

NEXT;

END;

// Restore Timer Controls for Working Status

TimeUpdate.enabled: = true;

Except

ON E: Exception DO

Application.Messagebox (

Pchar (Err_TimeUpdateTimerfail # 13 E.MESSAGE),

'Error',

MB_ICONERROR

);

END;

Because some tables have a large amount of data, there will be pauses when refreshing the grid, and the database structure is optimized, the redundant field is increased, and the refresh speed has been improved, but there is still a case where it is pauses one to two seconds. Since the primary concentration point in the database at first, the code is not taken into account the problem in execution efficiency. When discovered in a test, it is necessary to stop one to two seconds without any data grid needs to be refreshed. I immediately thought of this is definitely a problem with the execution efficiency of the code. After analysis, it is found that there is a data update, and the program must perform a while loop, and this while should traverse all records. When it discovers data update, a modification flag value is required and saves back to the database. So immediately let the programmer make changes:

1. Record of read-only flag value of 1, reduce the number of cycles; do not perform loops and update operations when there is no log value 1.

2, update all flag values ​​1 record in the end through the UPDATE statement

After modification, the pause phenomenon of the program disappeared, and the refresh efficiency is greatly improved.

The above code is a mistake that some programmers often makes mistakes, mainly not to take into account excess loops to increase many code runtime. By reducing the number of cycles, it is often an effective way to improve code execution efficiency.

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

New Post(0)