On Feb 12, 2007, at 05:17 UTC, Kem Tekinay wrote:
> I know this has probably been done to death, but I wanted emphasize
> how slow strings are in REALbasic for appends.
Right, because every append has to allocate a new string, then copy all
the bytes of the original string, followed by all the bytes of the
appended string. The same operations would be just as slow in C, BTW
(apart from loop and lock/unlock overhead).
To be fair, this is plenty fast on small strings, but of course gets
progressively slower on big strings, since there is more data to copy.
> Meanwhile, I took about an hour to create a custom class called
> FastString that uses an array to hold the parts of the string. By
> defining the Operator_Add and Operator_Convert methods, I can change
> s above from a string to a FastString, and the exact same code takes
> 0.1 seconds!
Sure. Of course what you have then is no longer a string, but a string
array. Making it automatically convert to a string is clever, but will
get REALLY slow if you use it as such multiple times. I doubt it's
worth it in general.
> If I rewrite the code to use a method within FastString,
> it takes half that:
>
> for i as integer = 1 to 50000
> s.Append( str( i ) )
> next i
>
> OK, so part of this is bragging, but the more serious question is, if
> I could do this in an hour, why can't RB use the same behavior in its
> native strings?
Because a string isn't an array of strings. An array of strings is,
though. I'd write your code like this:
Dim s() as String
for i as integer = 1 to 50000
s.Append str( i )
next i
..which, you notice, is exactly the same as your "FastString"-using
code, but doesn't require writing a FastString class. This
functionality is already built in. And when I want it converted to a
string, I do so with Join. This is better than implicit conversion, I
think, because it's an expensive step and you should be conscious of
when, where, and how often you're doing it.
If you tried using your FastString class everywhere you currently use a
string in a real app, I strongly believe you'll discover that your apps
get a lot slower overall. They're optimized for the artificial test
case you've presented, but do a ridiculous amount of work for the far
more common uses of strings. That's why RB strings don't work that
way; they're optimized for the common case, as they should be. And for
cases where you do need to join a bunch of stuff together, we have
string arrays and Join.
Best,
- Joe
>
> ______________________________________________________________________
> ____ Kem Tekinay
> (212) 201-1465 MacTechnologies Consulting
> Fax (914) 242-7294 http://www.mactechnologies.com
> Pager (917) 491-5546
>
> To join the MacTechnologies Consulting mailing list, send an e-mail
> to:
> [EMAIL PROTECTED]
>
>
>
>
>
>
>
>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>
>
--
Joe Strout -- [EMAIL PROTECTED]
Verified Express, LLC "Making the Internet a Better Place"
http://www.verex.com/
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>