Writing Efficient VB Software
Manyof you probably havent even gone through the set of Compiler options that areavailable. Ive done a little research and will attempt to explain whateveroptions the Visual Basic Compiler offers:
P-CodeVS Native Code:
Onething you might have noticed is that you can choose whether to compile yoursoftware as Native Code or P-Code (pseudocode). The default option is Nativecode and should be left like that.
Whenpressing the Start button in the Visual Basic IDE, the Visual Basic compileseach line into P-Code and interprets it. This is much faster than Native Code inthe IDE circumstance. By choosing to compile your software into P-Code, VisualBasic will store that code into an EXE where it will be interpreted line by linefor the rest of its life
Whatis Native Code?
NativeCode came out with Visual Basic 6.0. Running Native code is much faster thanrunning P-Code in an exe case. When compiling to Native Code, Visual Basiccompiles your software into the instruction set of the processor chip on yoursystem
Onething Ive noticed in Native Code, that unlike P-Code, it might give youstrange bugs. Many of you might not have faced such a problem but I have. Mycode runs perfectly in the IDE as it runs P-Code. But after I compile it, Isometimes (very rarely) get strange errors. Most of them occur while unloadingforms or some of them occur while displaying the Printer Dialog box over a modalform. I resolved these problems by putting the DoEvents statementin-between the code. I repeat, this happens only rarely and with some types ofsoftware.
Sowhat should you use? Leave it to Native Code.
OptimizeFor Fast Code:
Thisoption is only available if you decide to compile your software to Native Code.This tries optimizing the code to make it faster but it might compile larger.This should remain checked.
OptimizeFor Small Code:
Thisisnt recommended. This option is able to shrink the size of the software butby making it slower.
Thecompiler will simply convert the P-Code to Native Code without anyoptimizations. Only recommended if you are constantly getting errors in yourcompiled software.
Althoughthis isnt the default option for the Native Code compiler, I always check itand would recommend you check it to. Favor Pentium Pro allows you to favor thePentium Pro instruction set over the normal Pentium one. This could cause yoursoftware to run much faster on Pentium Pro and Pentium 2 + machines. Checkingthis does not mean that it will not run on other computers, it will just run alittle slower. A risk Im willing to take knowing many users use Pentiummachines these days and not 486s.
Mostof these settings can really increase the speed of your software, but leave itprone to bugs. Therefore, I dont recommend it. But I wouldnt say no. Ifyou have big loops or complex math involved that takes a long time, some ofthese optimizations could speed up your procedure dramatically. If you decide touse any of the Advanced Optimizations, you should test your software thoroughly.
- Assume No Aliasing: Could greatly speed up your code in loops but could cause problems if a variable is updated through a different name. What I mean is when you pass a variable by reference to a procedure which changes it, it could cause several errors or plain wrong results.
- Remove Array Bound checks, Remove Integer Owerflow Checks and Remove Floating Point Error Checks: All these are important checks that take place in your software. Error handlers pick up when such errors occur. But, by removing these checks, errors could go unnoticed. But, if you are sure that they won't affect your softwares final result, you could check them as they will greatly improve performance.
- Allow Unrounded Floating Point Operations: Checking this option will allow the compiler to manage floating point operations much more efficiently and faster. The only draw back of checking this would be that initial values are kept at a much higher precision. This could affect comparison, as normally the compiler would round off these values.
- Remove Safe Pentium FDIV Checks: Earlier, some Pentium CPU chips, under certain conditions, returned a bad result while manipulating floating point values. By default, the compiler checks for this error and it should not be removed.
Please dontforget to leave a comment. If you have anything to contribute to this article,please write me an email at Yash@apyl.com
Page 3 of 3