Slow Code Detection
In order to create a fast application, the code you write must be highly
optimized. When talking about code speed optimization you ought to know that
external factors (like video speed, network delays, disk activities) represent a
very important factor - however, there are usually points in program's execution
at which the code the main factor that causes slowness. When that's the
case, you can use a few techniques that can help you increase the speed of the code
you write:
-
Avoid Variant variables.
-
Use Integer variables and integer math.
-
Cache properties in variables.
-
Use For Each Next rather than For (index)...Next.\
-
Avoid using complex expressions as repetitive statements conditions
The CodeSMART Code Analyzer can be used to detect code lines who can support
further improvements in order to increase execution speed. Remember that the analysis
only reveals code that can be optimized and suggests possible improvements.
The task of implementing the changes is left to the programmer since the applicability
of the suggested updates may vary depending on the situation.
To set Slow Code detection options:
The Slow Code detection options can be set in the Code Analysis Options
dialog box:
-
In the CodeSMART main menu, select the Code Analyzer entry from the Reviews popup, or click the same button from the Other Tools toolbar to display
the Code Analyzer dialog-box (quick toolbar
and menu references are available).
-
Click on the detailed configuration link in order to display the Code Analyzer Options dialog and then activate the Slow Code tab.
-
Check/Uncheck the items you want to be executed/skipped by the
Code Analyzer. The Code Analyzer will suggest a possible improvement
for every encountered match.
-
Look for inefficient loop statement parameters: check this option
if you want the Code Analyzer to look for the certain types of inefficient
loop statements, further configuration being available by using the
below sub-options:
-
Report expressions containing qualifications used in loop
statements: check this option to instruct the Code Analyzer
to report loop statements containing one or more qualifications:
Test for any type of expression with qualification(s):
if you select this option the Code Analyzer reports all
the loop statements containing qualifications, for example:
Do While
Nodes.Count > 0
' inside loop...
Loop
Fix for the above example: Cache the
Nodes.Count expression ina variable before
the For statement, then use that variable as the
statement condition.
Test for expressions containing multiple qualifications
only: check this option to inform the Code Analyzer
to report only loop statements with multiple qualifications,
for example:
For i =
1 To
ImageList1.ListImages.Count
' inside loop...
Next
Fix for the above example: Cache the ImageList1.ListImages.Count
expression ina variable before the For statement,
then use that variable as the statement condition.
-
Report function calls used in loop statements: check
this option to make the Code Analyzer report all the loop statements
that contain function calls, for example:
Do While IsWrongPassword(Password)
And StillHasTries
' inside loop...
Loop
Fix for the above example: Cache the
IsWrongPassword(Password) function call ina variable
before the Do statement, then use that variable as the
statement condition.
-
Report expressions involving computations used in loop statements:
use this option to detect loop statements that include computations,
for example:
iCount = List1.ListCount
For i = 0
To iCount -
1
' inside loop code...
Loop
Fix for the above example: On the line above the For
statement, compute iCount as List1.ListCount - 1
and use iCount as For statement upper bound.
-
Look for slow (variant) VBA string functions: check this option
to detect variant VBA functions that can be replaced with their faster
string equivalent (e.g.
Chr can be replaced
with Chr$).
-
Report occurrences of the 'For..Next Counter' slow version of the
'For' statement: check this option to report all the For...Next
statements that use an explicit counter.
-
Click OK to make the changes permanent.
See also
Back To Top
|