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:



Longest test took: 33.9s

Bytes Per Append=7

 Append Count = 100
    RB = 1x
    ED = 3x
    CY = 0.99x
 Append Count = 1100
    RB = 1x
    ED = 14.5x
    CY = 2.8x
 Append Count = 2100
    RB = 1x
    ED = 24.7x
    CY = 4.02x
 Append Count = 3100
    RB = 1x
    ED = 151x
    CY = 30.6x
 Append Count = 4100
    RB = 1x
    ED = 280x
    CY = 54.4x
 Append Count = 5100
    RB = 1x
    ED = 408x
    CY = 70.8x
 Append Count = 6100
    RB = 1x
    ED = 513x
    CY = 87.9x
 Append Count = 7100
    RB = 1x
    ED = 618x
    CY = 109x
 Append Count = 8100
    RB = 1x
    ED = 710x
    CY = 123x
 Append Count = 9100
    RB = 1x
    ED = 756x
    CY = 135x


In addition to being having many useful functions, and being very fast, my FastString class has a long history of being used for almost everything. I've made it very easy to use, and added "the little things" that make using it much easier.

It is a mature class.

For example .FreeSize property can be set, as well as the .Size and .Length properties. These are convenient to set sometimes. I often will reduce .Length by 1 byte after a loop that added a number and then a space, to remove the final space. Reducing .Length doesn't take up any noticable time.

My plugin is also free for non-commercial use and is available for Mac, Win32, and Linux.

--
http://elfdata.com/plugin/



_______________________________________________
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>

Reply via email to