On Feb 11, 2006, at 7:03 AM, Theodore H. Smith wrote:
From: Seth Willits <[EMAIL PROTECTED]>
Date: Sat, 11 Feb 2006 00:28:26 -0800
If you're reading from a file and appending to a string a lot and
this string is going to grow pretty large, your application can
benefit significantly if you preallocate a block of memory and resize
it only when needed. With strings, each time you assign to a string
you're going to be reallocating a chunk of memory for the string. If
you cut down the allocations to one or just a handful, it can speed
up things significantly.
I wrote a very small quick class called CapacityString to show this
and a quick demo app to test it.
For a 1.7 MB file, reading in 2 KB chunks with a BinaryStream a
standard loop like that below takes about 8.5 seconds.
for i = 1 to length step 2048
s = s + bin.Read(2048)
next
Now by comparison, using a CapacityString, I can read a 27 MB file
and it takes 0.15 seconds. No joke.
My ElfData plugin could probably do the same thing in half the time.
I'd actually guess that reading things in 64KB chunks is faster than
2KB chunks, though, but that applies to both approaches.
With my ElfData plugin you'd do this:
dim fs as new FastString
const ChunkSize = 64*1024
for i = 1 to length step ChunkSize
fs.AppendString bin.Read(ChunkSize)
next
In addition .AppendString, the FastString class has a ton of useful
functions for creating text or data files.
http://www.elfdata.com/plugin/technicalref/FastString.html
For example, you can append one byte many times (useful for creating
neatly formatted printouts where you might want to align text into
columns). It can append UTF8 character codes. It can append integers
as hex, or integers as text.
I've got one example where .AppendIntegerAsText runs about 22X faster
than RB could manage even if your CapacityString were as fast as my
FastString class. (which it is not).
I've tested my FastString against a class by Charles Yeomans. I got
this result:
Your graphs suggest that my code has running time O(n^2) compared to
O(n) for yours. That doesn't seem right to me, especially in light of
my own testing that suggests that your function is about twice as fast
as mine, though I can cut that difference some by removing support for
encodings as in your plugin, and certain other tricks. But it appears
that RS has made this squabble moot, as Join now looks to be 50-100%
faster than your FastString.AppendString.
--------------
Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>