1. Supercharge your peek_string()

Platform: Windows


No more hunting character by character
through memory for that notorious
zero string terminatorsmile

This new way of peek'ing a zstring is typically
at least 5 times faster then the old way, and its
only one line of code smile

--first link to this windows function any way you see fit:

xlstrlen=link_c_func(kernel32,"lstrlen",{C_POINTER},C_INT)


--then the new Euphoria function becomes:

function peek_zstring(atom lpzString)
  return peek({lpzString,c_func(xlstrlen,{lpzString})})
end function

That's it!

I've named it "peek_zstring" to distinguish it from the old
function "peek_string", but more importantly to draw attention
to the importance of using it ONLY on zero terminated strings.
The speed increase should be REALLY drastic for very long
strings.


--typical usage:

atom lpFilename
sequence teststring

lpFilename=allocate_string("c:\\program files\\thisdir\\filename.txt")
teststring=peek_zstring(lpFilename)
printf(1,"%s\n",{teststring})
free(lpFilename)


On this particular string it benchmarked about 5 times faster
then a minimum form of the old peek_string().

Good luck with it!
--Al

new topic     » topic index » view message » categorize

2. Re: Supercharge your peek_string()

--- Al Getz <Xaxo at aol.com> wrote:
 
> This new way of peek'ing a zstring is typically
> at least 5 times faster then the old way, and its
> only one line of code smile

This is a lot like BSTR's, which are unicode strings (see my EuCOM stuff). 
Actually, the number of bytes are stored in the four bytes before the pointer
to the string.

=====
Matt Lewis
http://www14.brinkster.com/matthewlewis

new topic     » goto parent     » topic index » view message » categorize

3. Re: Supercharge your peek_string()

Al,
I've incorporated your peek_string idea into win32lib. Thanks for the idea.

It works fast because I suspect it's written in optimized assembler.

I have no idea what Matt is on about. These strings are just byte arrays
that are terminated with a #00 byte.

Just try this experiment...

  atom addr
  integer len1, len2

  addr = allocate(100)

  poke(addr, "abcdefghijklmnopqrstuvwxyz" & 0)

  len1 = c_func(xlstrlen, {addr})
  len2 = c_func(xlstrlen, {addr + 1})

  -- len2 --> 25
  -- len1 --> 26


------
Derek Parnell
Melbourne, Australia
"To finish a job quickly, go slower."

new topic     » goto parent     » topic index » view message » categorize

4. Re: Supercharge your peek_string()

Matt is refering to the BSTR string type which is used in Visual Basic--it
has the string length as a 4-byte integer prefixed to the string itself and
NO terminating 0.  VB uses these exclusively in its internal operations and
has functions to convert to or from zero-terminated strings for interacting
with the Windows API.

-- Mike Nelson, VB programmer (for cash), Eu programmer (for love)

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu