Use Binary Compare for Text
When comparing text, use binary compare instead of text compare. At run time, the overhead is much lighter for binary.
Minimize the Use of Format()
When you can, use
toString() instead of
format(). In most cases, it will provide you with the functionality you need, with much less overhead.
Use Charw
Use charw instead of char. The CLR uses Unicode internally, and char must be translated at run time if it is used. This can result in a substantial performance loss, and specifying that your characters are a full word long (using charw) eliminates this conversion.
Optimize Assignments
Use
exp += val instead of
exp = exp + val. Since
exp can be arbitrarily complex, this can result in lots of unnecessary work. This forces the JIT to evaluate both copies of
exp, and many times this is not needed. The first statement can be optimized far better than the second, since the JIT can avoid evaluating the
exp twice.
Avoid Unnecessary Indirection
When you use
byRef, you pass pointers instead of the actual object. Many times this makes sense (side-effecting functions, for example), but you don't always need it. Passing pointers results in more indirection, which is slower than accessing a value that is on the stack. When you don't need to go through the heap, it is best to avoid it.
Put Concatenations in One Expression
If you have multiple concatenations on multiple lines, try to stick them all on one expression. The compiler can optimize by modifying the string in place, providing a speed and memory boost. If the statements are split into multiple lines, the Visual Basic compiler will not generate the Microsoft Intermediate Language (MSIL) to allow in-place concatenation. See the StringBuilder example discussed earlier.
Include Return Statements
Visual Basic allows a function to return a value without using the
return statement. While Visual Basic 7 supports this, explicitly using
return allows the JIT to perform slightly more optimizations. Without a return statement, each function is given several local variables on stack to transparently support returning values without the keyword. Keeping these around makes it harder for the JIT to optimize, and can impact the performance of your code. Look through your functions and insert
return as needed. It doesn't change the semantics of the code at all, and it can help you get more speed from your application.
Tips for Porting and Developing in Managed C++
Microsoft is targeting Managed C++ (MC++) at a specific set of developers. MC++ is
not the best tool for every job. After reading this document, you may decide that C++ is not the best tool, and that the tradeoff costs are not worth the benefits. If you aren't sure about MC++, there are many good resources to help you make your decision This section is targeted at developers who have already decided that they want to use MC++ in some way, and want to know about the performance ASPects of it.
For C++ developers, working Managed C++ requires that several decisions be made. Are you porting some old code? If so, do you want to move the entire thing to managed space or are you instead planning to implement a wrapper? I'm going to focus on the 'port-everything' option or deal with writing MC++ from scratch for the purposes of this discussion, since those are the scenarios where the programmer will notice a performance difference.
Benefits of the Managed World
The most powerful feature of Managed C++ is the ability to mix and match managed and unmanaged code at the
expression level. No other language allows you to do this, and there are some powerful benefits you can get fr