Important alert: (current site time 7/16/2013 3:07:12 AM EDT)
 

article

Learn How To Make VB Handle Strings As Fast As C

Email
Submitted on: 5/14/2002 4:11:52 PM
By: Chris_Lucas  
Level: Advanced
User Rating: By 82 Users
Compatibility: VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel
Views: 51821
author picture
(About the author)
 
     If you want your strings routines to be as fast as possible, learn C. If you don't want to learn C use string mapping, its nearly as fast and pure VB. In this article I give you the silver bullet for strings. If strings are bottlenecking your code, I'm your new best friend. I've included a project that attempts to visualize the string for you. It also contains a word count comparision which uses four methods string mapping being the fastest by far (compiled).

 
winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/14/2002 4:15:00 PMChris Lucas

If you take the time to read this article, please leave your votes and feedback. Thanks and I hope you find this as useful as I do.
(If this comment was disrespectful, please report it.)

 
5/14/2002 8:32:14 PMBnyc77

Great code. I work in field that relies heavily on processing millions of lines of string data, and am always on the lookout for an easy way to write quick code to handle a specific need. This is a great tool for those of us who need string-crunching speed but don't have time to work up a C++ app. Cheers! Five stars for making my life that much easier...
(If this comment was disrespectful, please report it.)

 
5/15/2002 6:29:31 AMronniec23

excellent tutorial and I can see a million uses for this code.
(If this comment was disrespectful, please report it.)

 
5/15/2002 9:18:25 AMAdam Musson

Chris, this is excellent and in my opinion is one of the best submissions on PSC. It definitely deserves the award. I can't wait to see your class extending this to VB's other string functions. I for one will use it, and extend it if you miss any out, in my applications. If I do any work on it I'll send you the code. Cheers!!
(If this comment was disrespectful, please report it.)

 
5/15/2002 1:58:52 PMElias Barbosa

I am impressed. I liked your submission and your prompt response to every request made by the other PSC members that tried your code!

You should work at Tech Support, some where! :)

I just wanted to see some real world examples. For example, could your code be used to create a Replace function that would work faster than the native VB Replace Function?

You got my best vote...

(If this comment was disrespectful, please report it.)

 
5/15/2002 3:38:41 PMCharles Chadwick

It's refreshing to see things posted in the VB section that aren't "How To Use A List Box - Vote For My Code!" and "LeEt AoL HaCkErZ PrOgGiE!!!". This makes me actually want to start programming in VB again.
(If this comment was disrespectful, please report it.)

 
5/15/2002 9:58:36 PMBrian Morrison

Great stuff. I have been in the process of writing an application that does a ton of string processing, and am now busy putting this great code to use (already using replacements for mid, right and left, based on this)
(If this comment was disrespectful, please report it.)

 
5/27/2002 2:03:50 PMKyle

This is probably the best tool I have encountered on the net. It saved me mad time, and in turn allowed me to do my job faster, which made me more money. I'm all about more money, Chris. 5 stars. Use this, it rocks.
(If this comment was disrespectful, please report it.)

 
5/29/2002 3:23:04 PMJ. B.

I'd been trying to find ways to speed up the game I've been making and this looks like it could be useful. This earns you 5 from me. (Not to mention it could be useful in other apps too.)
(If this comment was disrespectful, please report it.)

 
8/26/2002 9:46:44 AMtibisan

MMMMMAAAANNNNNNNN!!!!! I've lost track on this submission(erased my history, after getting rondomly to this post), and i've searched the site for 2 weeks(not kidding). now i finally step here (by chance, what ironic!) to tell u that a love this code. i'll give my 5g's from all my heart.THANK YOU!!!!!
(If this comment was disrespectful, please report it.)

 
9/10/2002 2:48:24 PMChris Lucas

I've deleted a lot of your feedback in order to keep this page a managable size. I've noticed that this article continues to get a large number of hits and felt this would make things easier on the people just finding their way to the string revolution. Thank you all for all the overwhelming feedback and suggestions.
(If this comment was disrespectful, please report it.)

 
5/21/2003 4:01:00 AM

you should compare it with the string to ByteArray method, with is the standard in VB for fast working with strings.

Public Function FastWordCount2(strSample As String) As Long
Dim i As Long, j As Long
Dim TmpB() As Byte

TmpB = strSample ' String to bytes

For i = 0 To UBound(TmpB) Step 2
If TmpB(i) > 32 Then
FastWordCount2 = FastWordCount2 + 1
For j = i + 2 To UBound(TmpB) Step 2
If TmpB(j) <= 32 Then Exit For
Next j
'check for code with more than one character, double spaces, CrLf, etc.
For j = j To UBound(TmpB) Step 2
If TmpB(j) > 32 Then Exit For
Next j
i = j
End If
Next i
End Function



However, you're method is still 20% faster and dramaticaly faster if the number of words is increasing.

It is as fast as the old PEEKL-fuction.
(If this comment was disrespectful, please report it.)

 
11/16/2003 6:30:48 PMmike payne

Please help. I like the sound of your code, but how, for example, could i use it for creating faster versionsof the InStr and Mid() Functions of VB?
(If this comment was disrespectful, please report it.)

 
3/12/2004 6:28:05 AMMH


Possible BUG?:

In sub Append,
If the string lengths passed are any number bigger
than (m_BUFFER_PAGE_SIZE * 2), i.e. 139992,
then this routine only writes partial data to the
new buffer. loosing data in process.

m_BUFFER_PAGE_SIZE = 4096
m_BUFFER_PAGES = 99999

lng = 139992
partial write = m_BUFFER_PAGE_SIZE - pos = 4096
Remainder write = lng - partial = 135896

But the new buffer is redimed using
m_BUFFER_PAGE_SIZE which is only 4096

Lost = 131800 bytes during copying.

Please let me know if this is a bug or
I am in error and have missed something.

Thank you for your invaluable submission.
MH

(If this comment was disrespectful, please report it.)

 
3/12/2004 6:35:34 AMMH

My Appologies;
The last comment was meant for your cFastCat class. MH
(If this comment was disrespectful, please report it.)

 
3/12/2004 7:22:28 AMMH

Bug report:
In clsWordCount, in InStrWordCount function, in instr function located right after Do, if you change pos_curr + 1 to pos_curr + 2 then instr will produce results twice as fast as FastWordCount function. Am I missing something? Again thank you for your invaluable submissions. MH
(If this comment was disrespectful, please report it.)

 
6/15/2004 6:35:54 AMSteadler

The good thing is that when you change the string the array is also changed.
Gave you post a mid replcaement or something?
(If this comment was disrespectful, please report it.)

 
6/16/2004 4:26:27 AMSteadler

what's your pc man my computer is 1500 ghz and it takes double time to do these things that you do on the screenshot.uncompiled inst and string mapping have sslight different but compiled string mapping wins and overrulles!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.