|
-
May 29th, 2009, 11:34 AM
#8
Re: Tips for writing the cleanest, most efficient code? (Also a few related questions
There is a good article on VB6 string optimization over at Aivosto.com
Data types are important to maintain for both performance and memory reasons. This includes the inbuilt functions, ie. Mid is not the same as Mid$.
Long being 32-bit are natively faster with 32-bit processors which most people still use. This means that on every generic purpose Long is your choice for a numeric variable. You use other types only if you see the need.
However, VB automatically coerces data types to other types when it sees the need. When you do something as simple as 1 / 2 you end up with a Double, which is then again coerced back to Long. You can "sort of" get bitshifting with 1 \ 2 and thus end up maintaining the data type all the time.
Using some operators can sometimes be confusing, because they let you combine two incompatible data types. For example, you may do something weird such as "1" + 1. Now, somebody might think the result would be "2", but instead VB ends up doing a string concatenation. Another reason why keeping up to track of the data type you use is important! (This is also why Variant may silently be very evil for people who are new to classic VB.)
VB's own native string functions are very fast when used properly. For example, using Mid$ to move data around can beat Windows API CopyMemory (RtlMoveMemory) any time of the day (may involve some hacking around though). Some specialized functions are also very fast, such as StrReverse, InStr & InStrB (latter much faster, but harder to use; text searching can be done faster with a custom Boyer & Moore algorithm), Left$, Right$...
One especially slow one is IIf, simply avoid it! You can even write your custom IIf function and it'll be tons faster, even if you used Variant data type. DoEvents is another call that should be avoided: it does let your application refresh itself, but it also does tons of extra calls that are often unnecessary to just seemingly keep application alive. It can be used, but it needs some careful consideration.
On Error Resume Next can be quite evil, I'd suggest never to use it for more than against one line at a time.
Split can be beaten by pure VB6 code (you can find my QuickSplit if interested).
Returning arrays from functions and properties is slow, you can instead pass an array variable as a parameter to get things done faster.
If I recall correctly, ByVal for parameters is faster than the default ByRef, except for Strings and Objects. You should pass all numeric data types ByVal unless you wish to modify them in the procedure. You can pass strings ByVal if you want an extra variable and you don't need the original string that was passed except for some initial stuff.
VB6 lacks pointers. This means to get access to other variables avoiding copying memory requires hacking around, but it can be done. It may not always be pretty though. Key things to learn are Safe Array datatype (arrays of VB6 are internally in this format), StrPtr, VarPtr and a few API calls. Most often with VB6 the fastest & most efficient code is very long, while the shortest does the job slowly. There are exceptions. Finding the middle ground is a challenge in itself: making short yet efficient code that is easy to read requires experience.
The biggest issue with people who have a C/C++ background or similar is that when they get VB6, they suddenly kind of forget everything they knew when learning C/C++ and then blame VB6 for bad performance. VB6 doesn't in itself encourage to code in the same fashion, but basically in every case you can blame the programmer if you get slow code. You simply have to know what really happens when you write stuff, and if you don't know for sure, you aren't visiting a forum without a reason
Last edited by Merri; May 29th, 2009 at 11:44 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|