fastest,string,concatenation,method,youll,eve
Quick Search for:  in language:    
fastest,string,concatenation,method,youll,eve
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
RentACoder Stats

 Code:  lines
 Jobs: 0 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for RentACoder.
Click here to see a screenshot of this code!age
By sherif rofael on 8/19

(Screen Shot)

Click here to see a screenshot of this code!your age
By sherif rofael on 8/19

(Screen Shot)

Quad Click
By Skitzo Monk on 8/18


Afro Speak
By Skitzo Monk on 8/18


a 25% done 3D level editor
By Homee G on 8/18


Converts Seconds to Time (functions withour any errors)
By Sunil Wason on 8/18


Click here to see a screenshot of this code!PSC explorer-See the code from 0-38000 and more!!
By XasanSoft on 8/18

(Screen Shot)

Click here to see a screenshot of this code!VB IRCd (pure VB6 Code, no dlls, winsock.ocx is everything u need)
By Dennis Fisch on 8/18

(Screen Shot)

begginerproject
By Gary Seume on 8/18


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Fastest String Concatenation Yet In Visual Basic

Print
Email
 

Submitted on: 7/21/2002 12:11:35 PM
By: Chris Lucas 
Level: Intermediate
User Rating: By 25 Users
Compatibility:

Users have accessed this article 2159 times.
 

(About the author)
 
     This is the fastest string concatenation method you'll ever come across in Visual Basic. I use a novel buffering technique that leaves the competition choking on my dust.

This article has accompanying files

 
 
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.

Features

  • Faster than anything else (check out screenshot!)
  • No API calls
  • Fully configurable at runtime

Method

    The buffering scheme I've come up with is simplicity itself.  Rather than allocating increasingly large buffers only to move the previous buffer into the new buffer I've taken a different approach.   My buffer is a string array.  When one string fills up the class simply moves on to the next string in the array.   While this seems like a minor change it has two very profound effects on string concatenation.  First, this approach speeds concatenation up dramatically.  This is because buffers are never moved around which obviously takes time.  Second, much larger strings are possible because unlike conventional buffering schemes paged buffering does not require one huge and continuous chunk of memory.


Implementation

    Paged buffering string concatenation is best implemented as a class module.  I've named this class cFastCat and implemented it as follows:

Read / Write Properties

  • BufferPages - Sets/returns the number of pages contained in the buffer
  • BufferPageSize - Sets/returns the size of each buffer page

Read Only Properties

  • Value - Returns the current string in the buffer.  This is the default property of cFastCat
  • TotalBufferSize - Returns the amount of space in bytes the buffer is currently using.  Because the buffer does not by default free its resources this space can be different from the size of the string in the buffer
  • StringLength - Returns the length in characters of the string in the buffer
  • StringLengthB - Returns the length in bytes of the string in the buffer

Methods

  • Append - Concatenates the passed string onto the end of the buffer
  • Flush - Empties the buffer
  • ReleaseMemory - Frees all currently consumed resources


Don't Take My Word For it

    I've included everything in the zip.  A demonstration project is included as well as a full article complete with graphs of the timing results for a variety of calls.  Nothing is faster and all the proof is free for the downloading. Please leave your votes and feedback.


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 Winzipto 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.

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 7 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate 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
7/21/2002 12:15:11 PM:Chris Lucas
If you take the time to read my article please leave your votes and feedback.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/21/2002 1:24:44 PM:John Galanopoulos
Hi eisai ellinas?polu kali meleti.sinxaritiria.This is a very well documented project. Really usefull. 5 from me. Keep up.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/21/2002 3:15:51 PM:Bobert
This code is simply amazing thank you so much for your excellent contributions.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/21/2002 3:24:29 PM:lostcauz
OK, I must admit when I read the description I immediately thought of Robin Schuil's Speed String Concatenation which is awesome. I thought, "I bet he didn't compare this to that one." Well this bench even includes Robin's submission for comparison and beats it hands down! Wow! I am very pleased to see something of substance in this sea of "kiddie scripts" submitted here. Excellent work and 5 globes of course.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/21/2002 4:16:38 PM:Chris Lucas
My thanks, I don't like to post something unless I've compared it to everything else out there and it is actually faster, otherwise its a waste of everyone's time.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/21/2002 4:33:05 PM:StanTheMan
One word: Wow!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/21/2002 7:28:12 PM:Michael W.
I am very impressed. Excellent work :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 11:16:02 AM:ViperTECH Software
Yeah, you "sux" too =P Chris, excellent work! Couldn't have done any better, myself.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 11:53:22 AM:Aldo Vargas
Page Buffering is the fastest method when concatenating small strings. I developed a more simple algorithm that in average is faster than Chris' Page Buffering with large strings, and faster than all the other methods. Public Property Get Value() As String Dim i As Long, NumPages As Long Dim TotalSize As Long, PageLen As Long NumPages = PageCount - 1 For i = 0 To NumPages TotalSize = TotalSize + Len(strPages(i)) Next Value = Space$(TotalSize) If LenB(Value) = 0 Then Exit Property TotalSize = 1 For i = 0 To NumPages PageLen = LenB(strPages(i)) MidB$(Value, TotalSize, PageLen) = strPages(i) TotalSize = TotalSize + PageLen Next End Property Friend Sub Append(strAppend As String) If PageCount > MaxPages Then MaxPages = MaxPages + m_def_BUFFER_PAGES ReDim Preserve strPages(0 To MaxPages) End If strPages(PageCount) = strAppend PageCount = PageCount + 1 End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 7:22:28 PM:Jigglestheclown
About time we get a good example on here, thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 9:10:11 PM:Jero
I've been using this code snippet for a few months now... (see below) Can't remember where I found it... I think it uses the same method. Its very useful for converting a recordset into a string to send over TCP/IP... Its just a procedure you can copy in and use... The lngOffset is just a Long Variable. All you need do is declare it in the function you Call StrConcat in... All and don't forget to Trim$(strBuffer) after concaternation! It works about as fast as the code listed here... seemingly a little faster if you increase the reps to over 10000, there is really nothing in it though when I used Chris's timer to time this procedure against his. This proc seems a tad slower the lesser amount of repetitions there is?!? If you want to know why these methods are so fast, its because Visual Basic is seemingly slow allocating the 'space' for the appended string. I think from memory the function Space$() causes a bottleneck when called 1000's of times.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 9:11:10 PM:Jero
Private Sub StrConcat(ByRef strBuffer As String, ByRef strAppend As String, ByRef lngOffset As Long) Dim lngLen As Long lngLen = Len(strAppend) If (lngOffset + lngLen) >= Len(strBuffer) Then If lngLen > 4096 Then strBuffer = strBuffer & Space$(lngLen) Else strBuffer = strBuffer & Space$(4096) End If End If Mid$(strBuffer, lngOffset + 1, lngLen) = strAppend lngOffset = lngOffset + lngLen End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 10:17:51 PM:Chris Lucas
Thanks for all your votes and feedback. This last code sample here looks almost exactly like the conventional string buffering method so I doubt that it is faster than paged buffering, could be though, I'll test it later and let you all know. Anyway, I hope this class helps you out and I'll keep looking for ways to make it even faster.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 10:26:35 PM:Chris Lucas
So I'm curious, do we really need to concatenate strings this fast and this big? I'm all about chasing that last 1% but I'm wondering if anyone else really cares...and if so what are you doing that you make such massive strings?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/22/2002 10:43:05 PM:MerliN Projects
Awesome
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/2002 3:10:41 AM:Dreamlax
Chris Lucas: I suppose if you wanting to join a whole bunch of things from a database... but even so.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/2002 10:05:55 AM:Hawkeye
Loved the code. Excellent work. 5 globes. I am working on an enhancement that will allow searching, and specific location of individual strings appended during the course of program use, by implementing a dictionary object that holds a type structure defining specific information for each buffer and each string added. You call it 'page buffering', but, in essence, it is just a series of buffers, designated by size, in a an array, is it not? These could not be compared, for instance, to an SQL Server data page? Thanks for the great code.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/2002 11:04:50 AM:Chris Lucas
Yeah, I call it paged buffering because I think its an apt physical analogy of whats really happening. I'm glad so many people are using and extending this method, does my heart good to see code actually used.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2002 10:26:20 AM:Paul Caton
Chris, re what for? I'll be using it for base64 decoding... but not this week. Possibly also to concatenate html strings in a web class.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2002 12:44:20 PM:mark jackson
I've been following the 'fast strings' race for a while now, and enjoy the healthy and usefull competition. It's nice when parsing text to be able to manipulate it quickly. Love the idea of a class module to handle the details. The more features to play with VB strings the better.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | RentACoder Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.