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).
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:
Re-scan downloaded files using your personal virus checker before using it.
NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
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
Terms of Agreement:
By using this article, you agree to the following terms...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
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.)
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.)
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.)
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.)
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.)
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.)
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.)
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.)
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.)
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.
(If this comment was disrespectful, please report it.)
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.)
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.)