It is currently Sun Oct 13, 2019 9:51 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 54 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: Fast string
PostPosted: Fri Mar 28, 2014 12:56 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
In my opinion, working with strings in PB, very slow (snail crawls faster than operations are performed with strings).
The example is very slow work with a string.
Code:
DisableDebugger

Str.s
#Text = "1234567890"

Time = ElapsedMilliseconds()

For i=1 To 100000
  Str + #Text
Next i

MessageRequester("", StrF((ElapsedMilliseconds()-Time)/1000, 3))
Similar code with the string in memory is performed in a split second.
Code:
DisableDebugger

*Str = AllocateMemory(10)
Len = 10
Pos = 0
#Text = "1234567890"

Time = ElapsedMilliseconds()

For i=1 To 100000
  Size = StringByteLength(#Text)
  If Pos+Size+4>Len
    Len+Size+4
    *Str = ReAllocateMemory(*Str, Len)
  EndIf
  Pos+PokeS(*Str+Pos, #Text, Size)
Next i

EnableDebugger

Result.f = (ElapsedMilliseconds()-Time)/1000
ShowMemoryViewer(*Str, Len)
MessageRequester("", StrF(Result, 3))
I assume that the reason for the slow work with strings in the string length is calculated for each access to string.
If the in beginning of the string to keep it length and not be calculated for each access to the string, it should increase performance.

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 1:13 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Mar 21, 2006 12:31 am
Posts: 1773
Location: Canada
What snail species you in reference too? :P

The speed of land snail is 12.2 meters per hour. The fastest specie of landsnail is the common garden snail (Helix aspersa) moving at 20 meters per hour. The fastest recorded was by the Helix aspersa moving at 48.3 meters per hour. :wink:

_________________
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 2:18 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
Incidentally, in SpiderBasic, the first code is work fast (20 milliseconds). :shock: In PureBasic a very long time! :?

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 2:48 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 1021
Location: 'stralia!
It's not a Problem in JavaScript (at least on some implementations) because there exist optimizations.
The problem that it is so slow in PB is because of the continuous allocation and deallocation of memory.

Search for StringBuilder on the forum to find a fast way. :wink:
(This is not generally a PureBasic "problem". Firstly because it's not a problem at all but what is to be expected,
and secondly because it is the same case in most programming languages).

_________________
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 3:10 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
Shield wrote:
The problem that it is so slow in PB is because of the continuous allocation and deallocation of memory.
I think that problem not in it.
In the second code is also allocated and deallocation memory
Code:
*Str = ReAllocateMemory(*Str, Len)
but the program works fast.
In my opinion, the problem is that every access string, measured its length. And this is done by iterating through all characters and search for zero bytes. It's a slow process.
Need to store the string length (for example, as stored, the size of the array) and not recalculate it every time.

This confirms the experiment.
If the second code to add a function MemoryStringLength(), then it will work just as slowly as the first code.
Code:
DisableDebugger

*Str = AllocateMemory(10)
Len = 10
Pos = 0
#Text = "1234567890"

Time = ElapsedMilliseconds()

For i=1 To 100000
  Size = StringByteLength(#Text)
  If Pos+Size+4>Len
    Len+Size+4
    *Str = ReAllocateMemory(*Str, Len)
  EndIf
  Pos+PokeS(*Str+Pos, #Text, Size)
  MemoryStringLength(*Str) ; This is the main reason the slow work With strings!
Next i

EnableDebugger

Result.f = (ElapsedMilliseconds()-Time)/1000
ShowMemoryViewer(*Str, Len)
MessageRequester("", StrF(Result, 3))
Measuring the length of string each time you access the main reason for the slow work of all string functions!

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 3:17 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 1021
Location: 'stralia!
I don't think that PB uses ReAllocateMemory to create a new string buffer.
ReAllocateMemory can reuse the same buffer if there is more space available at the end
whereas AllocateMemory will always create a new buffer (so everything has to be copied).

I can't comment on the speed of MemoryStringLength(*Str) though,
but it seems to be rather slow as your experiment confirms.

_________________
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 4:03 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
When added in string, use function HeapReAlloc(), the same as in ReAllocateMemory().
The memory is not deallocation and allocated.
Works similarly ReAllocateMemory().
In my opinion, slow work, due to of the function strlen().

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 4:35 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 1021
Location: 'stralia!
Yes, but PureBasic is using HeapAlloc, not HeapReAlloc, while concatenating strings.

_________________
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 6:05 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
Function string concatenation.
Code:
int __stdcall sub_100021B0(LPVOID *a1, int a2)
{
  int result; // eax@4
  int v3; // [sp+0h] [bp-4h]@1

  v3 = dword_10004150 - a2;
  if ( *a1 )
    *a1 = HeapReAlloc(dword_10004158, 0, *a1, v3 + 5);     // < ----- HeapReAlloc.
  else
    *a1 = HeapAlloc(dword_10004158, 0, v3 + 5);
  sub_100022B0(*a1, (char *)lpMem + a2, v3);
  result = a2;
  dword_10004150 = a2;
  return result;
}

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 6:25 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 1021
Location: 'stralia!
Interesting. How did you produce this? I quickly checked with a disassembler
before posting and I could have sworn it said HeapAlloc. :|

_________________
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 6:49 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
IDA Pro. viewtopic.php?f=17&t=58423

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 7:40 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 1021
Location: 'stralia!
Well, that's a little bit far out of my wallet's league. Thanks for the link, though. :)

_________________
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Fri Mar 28, 2014 8:02 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
Then compile this code.
Code:
s.s+" "
And open exe-file in editor.
Find in text, the word HeapReAlloc.

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Sat Mar 29, 2014 1:03 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1229
Location: Germany, Glienicke
Fast string is implimented es CopyMemoryString()

Code:
Str + #Text

is converte to:
Code:
Str = Str + #Text

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast string
PostPosted: Sat Mar 29, 2014 2:14 am 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3075
Location: Boston, MA
STARGÅTE wrote:
Fast string is implemented as CopyMemoryString()
Yes, without it, string concatenation is unusable for large writes. I use Join() when speed is required.

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 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