devxlogo

Optimize Code

Optimize Code

I often see questions in the VBPJ forum about comparative speedsfor different methods of performing actions in code. In many cases, a simplegeneralization 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 wishto test:

 TStart! = Timer 

At the end of the segment, print the elapsed time to the debugwindow:

 Debug.print Timer - TStart! 

By doing such time-testing you will be better able to choose betweensimilar ways of doing things. In most cases, for example, the CStr andStr functions are much faster than the Format or Format$ functions. Likewise,the If/Elseif sequence will execute much faster than similar code usingthe IIf or Choose functions, probably because of the added type checkingdone by the functions. But IIf and Choose often allow much shorter, clearersubroutines. By performing simple time tests, you can better decide onthe appropriate code style.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist