It is currently Thu Aug 06, 2020 10:54 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 63 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
Author Message
 Post subject: Re: Fast string
PostPosted: Wed May 29, 2019 8:32 am 
Offline
Addict
Addict

Joined: Thu Apr 18, 2019 8:17 am
Posts: 949
+1 for faster native strings.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Wed May 29, 2019 6:47 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Jun 23, 2014 1:18 pm
Posts: 212
@forumuser

I don't know what you mean… do you have an example?

Btw. These are my string-tools at the moment.

Code:
EnableExplicit

Procedure StringAppend(*sb,string.s)
   If string=""
      ProcedureReturn *sb
   EndIf
   Protected size,*Offset
   If *sb
      size=MemorySize(*sb)
      *Offset=size
   EndIf
   size+StringByteLength(string)
   *sb=ReAllocateMemory(*sb,size,#PB_Memory_NoClear)
   PokeS(*sb+*offset,string,-1,#PB_String_NoZero)
   ProcedureReturn *sb
EndProcedure

Procedure AppendString(*sb,string.s)
   ProcedureReturn StringAppend(*sb,string)
EndProcedure

Procedure.s SBToString(*sb)
   If *sb
      ProcedureReturn PeekS(*sb,MemorySize(*sb)/SizeOf(Character),#PB_Unicode)
   Else
      ProcedureReturn ""
   EndIf
EndProcedure

Macro SB2String(n)
   SBToString(n)
EndMacro

Procedure SBClear(*sb)
   If *sb
      FreeMemory(*sb)
   EndIf
   *sb=0
EndProcedure

Macro SBFree(n)
   SBClear(n)
EndMacro

ImportC ""
   wcsstr.i(*str1, *str2)
   _wcslwr.i(*cs)
   wcslen.l(*cs)
EndImport

Procedure FastStringSplit(sString.s, sDelimiters.s, Array StringArray.i(1), Casesense = 1) 
   
   Protected String = @sString
   Protected Delimiters = @sDelimiters
   
   Protected StringSize = wcslen(String) * 2
   Protected DelimitersSize = wcslen(Delimiters) * 2
   
   Protected aString = AllocateMemory(StringSize + 2):CopyMemory(String,aString,StringSize)
   If Not aString:ProcedureReturn 0:EndIf
   
   If Casesense = 0
      _wcslwr(String)
      _wcslwr(Delimiters)
   EndIf
   
   Protected String2 = String
   Protected ElementStart ,ElementSize
   
   Protected aItem
   Repeat   
      
      String2 = wcsstr(String2,Delimiters)
      If String2 = 0
         ElementSize = StringSize - ElementStart
      Else
         ElementSize = String2 - (String + ElementStart)
      EndIf
      
      If ArraySize(StringArray()) < aItem : ReDim StringArray(aItem +99):EndIf
      
      StringArray(aItem) = aString + ElementStart
      aItem + 1
      
      PokeW(aString + ElementStart + ElementSize, 0)
      
      ElementStart + ElementSize + DelimitersSize
      
      String2 + DelimitersSize
      
   Until String2 = DelimitersSize
   
   If ElementSize = StringSize
      FreeMemory(aString)
      If ArraySize(StringArray())=1
         StringArray(0)=@sString
      EndIf
      ProcedureReturn 0
   EndIf
   
   ReDim StringArray(aItem-1)
   
   If ArraySize(StringArray())=0
      StringArray(0)=@sString
   EndIf
   ProcedureReturn aString
EndProcedure

Procedure.s makeParsebleNumber(value.s,post.s="",prefix.s="")
   Protected result.s=Trim(ReplaceString(Value,#DQUOTE$,""))
   If post<>""
      result=ReplaceString(result,post,"")
   EndIf
   If prefix<>""
      result=ReplaceString(result,prefix,"")
   EndIf
   Protected pp=FindString(result,".")
   Protected kk=FindString(result,",")
   If pp<kk
      result=ReplaceString(result,".","")
      result=ReplaceString(result,",",".")
   Else
      result=ReplaceString(result,",","")
   EndIf
   ProcedureReturn Trim(result)
EndProcedure


Procedure.d extValD(value.s,post.s="",prefix.s="")
   Protected result.s=makeParsebleNumber(value)
   ProcedureReturn ValD(Trim(result))
EndProcedure

Procedure.f extValF(value.s,post.s="",prefix.s="")
   Protected result.s=makeParsebleNumber(value)
   ProcedureReturn ValF(Trim(result))
EndProcedure

Procedure.b isLong(value.s)
   ProcedureReturn Bool(Str(Val(value))=value)
EndProcedure


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Wed Jun 05, 2019 8:42 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Aug 07, 2003 7:01 pm
Posts: 3142
Location: United Kingdom
+1

_________________
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Wed Jun 05, 2019 8:52 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1773
Location: Uttoxeter, UK
+1

_________________
DE AA EB


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Wed Aug 14, 2019 4:31 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 743
Location: Linz, Austria
+1

_________________
Et cetera is my worst enemy


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Wed Aug 14, 2019 5:15 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 717
+1


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Thu Aug 15, 2019 5:07 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 1673
+1


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Aug 16, 2019 4:40 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 297
Location: NL
Possibly take a look at http://bstring.sourceforge.net (or fork https://github.com/msteinert/bstring) for inspiration. Better String Library (bstrlib) for c.

Or https://github.com/antirez/sds . Simple Dynamic Strings (SDS), a c-string compatible optimized library by putting a header with length info in front of the actual c string which makes it an easy insert into PB I guess?


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Aug 16, 2019 3:00 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 1673
I do not work with very large strings, but mostly under 64 characters. It would be nice if we could set the minimum length for the initial allocation, so there would typically be no additional allocations.

I'm also considering using ropes, skeptically. This might be useful, especially for those of you using very long strings. Basically, they are a List of shorter strings treated as a single string. Ideal for long sets, since it can eliminate your large repetitive allocations. Likely overkill for me though...


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Mon Nov 11, 2019 8:59 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Mar 25, 2004 2:15 pm
Posts: 721
Location: Spain
:lol: I thought I'd be alone in this fight.

+1 to have a faster native option of string concatenation

_________________
PB 5.7x, PureVision User.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Tue Jan 14, 2020 1:06 pm 
Offline
Addict
Addict
User avatar

Joined: Tue May 08, 2007 1:27 pm
Posts: 2754
Location: Chiba, Japan
This reminds me of the "Shlemiel the painter’s algorithm" from years (decades? ago)

https://www.joelonsoftware.com/2001/12/11/back-to-basics/
Joel had a long and interesting rant about this topic. :lol:

It'd be nice for PB to perform this better since I've come to expect good performance under the hood on these kinds of things from the PB Team but it is something that can be (and often is) handled by the developer

+1

_________________
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Sat Jan 25, 2020 1:35 pm 
Offline
User
User

Joined: Wed Aug 28, 2019 1:01 pm
Posts: 71
+1 for faster strings


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Sat Feb 08, 2020 6:45 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Nov 26, 2015 6:52 pm
Posts: 181
Location: Italy
Quite interesting, in this whole thread no one mentioned (nor tested) that PureBasic natively supports Fixed Strings ".s{Length}".

I'v rarely seen fixed-size strings used in real case examples, and I wonder if it has any benefits over "normal" strings.

Since the ".s{Length}" is a fixed width string, does it use Zero terminator? or does it store it's length somewhere?

I never quite understood why this alternative notation was added, and whether it has any benefits over the other notation.

Any ideas?

_________________
The PureBASIC Archives:
FOSS Resources:


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Sat Feb 08, 2020 8:29 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1126
Tristano wrote:
... I wonder if it has any benefits over "normal" strings.

A 'normal' string is a pointer to a pointer to a memory area. Every time you change the string, this memory area is newly allocated.

A fix string is a pointer to a memory area that never changes. So the background overhead is much less. In structures a fixed string is stored directly in the structure.

Tristano wrote:
Since the ".s{Length}" is a fixed width string, does it use Zero terminator? or does it store it's length somewhere?

With a fixed string you can use the full specified length. A reservation for a null terminator is not necessary. However, you can make the string shorter by using a null terminator.

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Sat Feb 08, 2020 9:54 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 1673
Quote:
A 'normal' string is a pointer to a pointer to a memory area. Every time you change the string, this memory area is newly allocated.

I believe it's only reallocated when necessary, not every time.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 63 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye