10 Things to Avoid When Moving From VB6 to VB.NETLink Round-Up for 8/9/07

String Class vs. Microsoft.VisualBasic Namespace - Part I

August 8th, 2007

scaleIn this article, I’ll look at some string functions in the Microsoft.VisualBasic namespace. People often malign the Microsoft.VisualBasic Namespace, sometimes unfairly, as being slow and obsolete, Let’s take a look at some of them and see how they function and perform vs. similar .NET functions in the String class. I’m going to use Dylan Copeland’s test program as my speed testing platform for this article.

StrReverse

In my previous article 3 Handy VB.NET String Functions You Can Use I had a function to reverse a string. I hadn’t realized that our old pal, StrReverse, had been ported over from VB6 and is a member of the Microsoft.VisualBasic Namespace. The question is, does it perform better than the standard .NET method I used or is it slower?

Here’s what I found. On the initial run the StrReverse is much faster, almost twice as fast. However, on subsequent runs presumably after my Reverse string function has been just-in-time compiled, they’re almost identical in performance. On multiple runs where each function was called 10000 times, the .NET method is about 2-3% faster on average.

So, StrReverse has a significant speed advantage for a single call but is only marginally slower on repeated calls. My take is that unless you’re a .NET purist who loathes the Microsoft.VisualBasic Namespace, use StrReverse. It won’t hurt performance and it is more understandable and maintainable than a separate function.

Trim, RTrim, LTrim

Trim is a separate function in VB.NET as well as a built-in member function of the string class. The String class function also has the advantage of having an overload that allows you to specify a character array of values to trim. How is the performance though?

In this test, the Microsoft.VisualBasic Trim function performed poorly on the first call as compared to the String class function. However, after this initial call, their performance was virtually identical, even over several thousand runs.

On this function, my opinion is that the built-in String class function is a better choice since it provides an essential overload. However, there is very little difference performance-wise between the two. The TrimEnd and TrimStart String functions are also effective replacements for RTrim and LTrim.

Mid, Right, Left

These standard string functions have been around since the early days of BASIC. However, in .NET, the built-in String class offers the SubString function as an option. However, there is no direct equivalent to Right and Left.

One key difference between Mid and SubString is that Mid is one based and SubString is zero based so far as the starting position goes. This can throw some people off so be careful about switching between methods.

Another important difference is in how the two functions deal with ranges. With Mid, if the provided length parameter is longer than the string, the whole string is returned. With SubString, if this value is too large you get a nasty, performance killing, ArgumentOutOfRange exception. So, if you use SubString you have to be extra careful about the string length vs. the parameter value.

Now, let’s check out their performance. Once again, String class is faster on the first call but, after that, the speed is nearly identical. There is no performance penalty beyond this for these functions.

I’m kind of torn on this one. I like the built-in neatness and zero-basing of the SubString function but I don’t like how quick it is to throw an exception if things aren’t perfect. Therefore, I’ve found myself using Mid when reading in unknown raw data, which is most of the time, in order to avoid dealing with unexpected exceptions or adding considerable code to do unnecessary checking. I also like to use Right from time to time too.

LCase, UCase

The last Microsoft.VisualBasic namespace string functions I’ll take a look at in this article are LCase and UCase. These functions make the case of a string either all lower or all upper case. The String class has these equivalent functions: ToLower and ToUpper. There is no functional difference so how does the performance look?

In my tests, the built-in String function is about 5-7% faster than these functions. While this isn’t a huge performance hit unless you’re running 1000’s of iterations, it does point out that this function is best handled by the String class internally.

I’ll cover some additional functions in the near future. Let me know which ones you would like to see discussed. Also, if you have any questions or observations, please leave me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (No Ratings Yet)
Loading ... Loading ...

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives