In programming, the program execution efficiency is often low, and sometimes the problem occurs in the loop body. The following example shows the issues that need attention in programming.
The main function of the Code in this example is to regularly check whether several tables in the database have been updated. If any, the corresponding data grid in the display window is automatically refreshed. Check whether the data of these tables is updated by checking whether the value of a flag field corresponding to the table name in a data table is 1. If it is 1, it indicates that the table has been updated, the data mesh needs to be refreshed.
The following code is the code before optimization (this code runs in the OnTime event of the Timer control ):
Try
// Stop Timer control timing
Timeupdate. Enabled: = False;
//// Read the data of the table in the refresh state.
// ADOQryReadUpdate. Refresh;
// ADOQryReadUpdate. Requery ();
ADOQryReadUpdate. Close;
ADOQryReadUpdate. Open;
ADOQryReadUpdate. First;
// Cyclically judge that the table has been updated
With ADOQryReadUpdate do
While not Eof do
Begin
BFlag: = False;
// Determine whether the flag field of the corresponding table name is 1. If it is 1, refresh the corresponding data grid.
If (FieldByName ('flag'). asinteger = 1)
And (FieldByName ('channelid'). AsInteger = LoginChannelID) then
Begin
SName: = FieldByName ('name'). asstring;
// Refresh the data grid code, skipped here
......
If bFlag then
Begin
// Modify the flag value of the corresponding table to 0
Edit;
FieldByName ('flag'). asBoolean: = False;
Post;
End;
Next;
End;
// Restore the Timer control to the working state
Timeupdate. Enabled: = True;
Except
On E: Exception do
Application. MessageBox (
Pchar (Err_TimeUpdateTimerFail + #13 + E. Message ),
'Error ',
MB_ICONERROR
);
End;
Because the data volume of some tables is large, there will be a pause when refreshing the grid. After optimizing the database structure and adding redundant fields, the refresh speed has been improved, but there is still a pause of one to two seconds. Because the primary focus was on the database at the beginning, the code execution efficiency was not taken into account. When a test finds that no data mesh needs to be refreshed, it will take one to two seconds to pause. I immediately thought this was definitely a problem with code execution efficiency. After analysis, it is found that no matter whether there is data update, the program will execute a While loop, and this While will traverse all records. when the data is found to be updated, you also need to modify the flag values one by one and save them back to the database. So I immediately asked the programmer to make the following changes:
1. Read-only records with a flag value of 1 reduce the number of cycles. If there is no record with a flag value of 1, no cycles or updates are performed.
2. UPDATE all records whose flag value is 1 through the UPDATE statement.
After modification, the pause of the program disappears, and the refresh efficiency is greatly improved.
The above code is a common mistake made by some programmers, mainly because the excessive loop will increase the code running time. Reducing the number of loops is often an effective way to improve code execution efficiency.