PureBasic Forum
http://forums.purebasic.com/english/

Fast string
http://forums.purebasic.com/english/viewtopic.php?f=3&t=58892
Page 5 of 5

Author:  pdwyer [ Sun Feb 09, 2020 3:31 am ]
Post subject:  Re: Fast string

In the past I often worked around this with loops inside loops with a mod(loop,100) or something to build smaller strings first which was reasonably effective but recently, after a little thought I tried something else.

In many cases, these sorts of long string concatenation loops are for preparing data for disk (not always of course). Many times I've hit this is a loop to prepare lines of text or csv for a data dump, export or log dump. In fact I had this come up just the other day and was seeing significant slowdown as the string length went over 100's of kb and was just about to add the mod() trick and thought, "why not just stream straight to disk?" and then had that auto-thought (because disk is slower than memory!).

So I tried it, Openfile(), loop the strings being built to hold a single line of CSV, WriteString(), end loop, CloseFile().

All delays went away and it scales a lot larger.

I would guess that if your target was a network destination and streaming data the situation would be similar.

Perhaps if you are building huge strings you should ask what your ultimate goal of the built string is as there may be a way to stream it. If strings of data get really huge then streaming will be a better plan in most cases anyway.

I'm sure there will always be cases where fast string cat of large strings is needed and I'm sure people here are hitting them, but perhaps there are less than I first imagined.

Author:  HeX0R [ Tue Feb 11, 2020 10:06 pm ]
Post subject:  Re: Fast string

No one thought about CopyMemoryString()?
Code:
DisableDebugger


Str.s   = ""
Text.s  = "1234567890"
Time    = ElapsedMilliseconds()
*Buffer  = AllocateMemory(2000002)
*Pointer = *Buffer
CopyMemoryString(Text, @*Pointer)
For i=1 To 99999
  CopyMemoryString(Text)
Next i

MessageRequester("", StrF((ElapsedMilliseconds()-Time)/1000, 3))
EnableDebugger
Debug PeekS(*Buffer)

Author:  BarryG [ Tue Feb 11, 2020 10:14 pm ]
Post subject:  Re: Fast string

HeX0R wrote:
No one thought about CopyMemoryString()?

Nice, but all the tricks like this shouldn't be needed.

pdwyer wrote:
if you are building huge strings you should ask what your ultimate goal of the built string is as there may be a way to stream it

Nah, PureBasic should just build them faster, as HeX0R just showed.

Page 5 of 5 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/