|
|
|
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.
| |
Download 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 |
|
|
Your Vote! |
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.
|
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.
|
7/21/2002 3:15:51 PM:Bobert This code is simply amazing thank you
so much for your excellent
contributions.
|
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.
|
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.
|
7/21/2002 4:33:05 PM:StanTheMan One word: Wow!
|
7/21/2002 7:28:12 PM:Michael W. I am very impressed. Excellent work :)
|
7/22/2002 11:16:02 AM:ViperTECH Software Yeah, you "sux" too =P Chris,
excellent work! Couldn't have done any
better, myself.
|
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
|
7/22/2002 7:22:28 PM:Jigglestheclown About time we get a good example on
here, thanks
|
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.
|
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
|
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.
|
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?
|
7/22/2002 10:43:05 PM:MerliN Projects Awesome
|
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.
|
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.
|
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.
|
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.
|
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.
|
|
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. |
|