Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use VB's Timer Function to Optimize Code

I often see questions in the VBPJ forum about comparative speeds for different methods of performing actions in code. In many cases, a simple generalization of which is fastest cannot be made. VB's Timer function, however, allows testing your code as you go. Declare a Single variable, TStart! and use this line at the beginning of the code segment you wish to test:
 TStart! = Timer 
At the end of the segment, print the elapsed time to the debug window:
 Debug.print Timer - TStart! 
By doing such time-testing you will be better able to choose between similar ways of doing things. In most cases, for example, the CStr and Str functions are much faster than the Format or Format$ functions. Likewise, the If/Elseif sequence will execute much faster than similar code using the IIf or Choose functions, probably because of the added type checking done by the functions. But IIf and Choose often allow much shorter, clearer subroutines. By performing simple time tests, you can better decide on the appropriate code style.
William Storage
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date