Get String Characters 4x Faster than Mid()

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
fxm
Posts: 8516
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Get String Characters 4x Faster than Mid()

Postby fxm » Dec 06, 2018 16:17

With 'ppz' a Zstring Ptr Ptr:
ppz[i][j]
provides a sub-zstring

With'pz' a Zstring Ptr:
pz[i + j]
provides also a sub-zstring

Only by assigning such an expression to an ubyte allows to initialize it (the famous syntax decried by many).
MrSwiss
Posts: 2862
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Get String Characters 4x Faster than Mid()

Postby MrSwiss » Dec 06, 2018 16:30

I'm only talking about 'getters', nothing to do with 'initializing' or 'setters'.
Munair
Posts: 802
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Get String Characters 4x Faster than Mid()

Postby Munair » Dec 06, 2018 16:31

When iterating through a zstring the pz[a][b] notation would allow for comfortable assignment leaving the zstring pointer (pz) at the beginning of the string data. In Pascal we see the equivalent with PChar whereby the string pointer itself is moved across the string memory block to conveniently copy string parts. Different programming situations may benefit from different notations, and FB is quite flexible in this respect.
fxm
Posts: 8516
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Get String Characters 4x Faster than Mid()

Postby fxm » Dec 06, 2018 16:43

So your previous post:
MrSwiss wrote:on topic, again:

pz[i][j] notation, should IMO only be used on a:

ZString Ptr Ptr (a ZString Ptr array, with char access)
used as follows:
*pz[i] for the whole ZString and,
pz[i][j] for a char (UByte)

on ZString Ptr, the notation should be:
*pz for the whole ZString and,
pz[i + j] for a char (UByte)
is wrong.
MrSwiss
Posts: 2862
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Get String Characters 4x Faster than Mid()

Postby MrSwiss » Dec 06, 2018 16:55

Munair wrote:When iterating through a zstring the pz[a][b] notation would allow for comfortable assignment leaving the zstring pointer (pz) at the beginning of the string data.

Sorry but, you really have some funny ideas (about FB, at least).
The ZString Ptr (because, there is only one ZString) isn't ever modified, anyway.
The index (one only!) gives you a reference to a 'char' (ByRef As UByte).

Therefore, pz[i][j] should be witten pz[0 + n], if you always want to start with 0.
Munair
Posts: 802
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Get String Characters 4x Faster than Mid()

Postby Munair » Dec 06, 2018 17:41

MrSwiss wrote:The ZString Ptr (because, there is only one ZString) isn't ever modified, anyway.

One can change the zstring ptr any way that one sees fit while within scope:

Code: Select all

dim s as string = "FreeBASIC forever"
dim pz as zstring ptr = sadd(s)

pz[2][0] = 105
pz[2][1] = 105

print *pz

pz += 2
pz[0] = 101
pz += 1
pz[0] = 101
pz -= 3

print *pz
MrSwiss
Posts: 2862
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Get String Characters 4x Faster than Mid()

Postby MrSwiss » Dec 06, 2018 18:02

First: you are quoting out of context (makes no sense at all).
Second: you are freely mixing, ZString Ptr (changing) & indexing a ZString (dereferenced ZString Ptr).
The two of them are, so to speak: "a different pair of shoes".
What I've been referring to, is only: indexing a ZString (ZString Ptr = unchanged)

You are of course free, to shoot yourself in the foot ... (FB allowes it).
Last edited by MrSwiss on Dec 06, 2018 18:17, edited 1 time in total.
Munair
Posts: 802
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Get String Characters 4x Faster than Mid()

Postby Munair » Dec 06, 2018 18:16

MrSwiss wrote:First: you are quoting out of context (makes no sense at all).
Second: you are freely mixing, ZString Ptr (changing) & indexing a ZString (dereferenced ZString Ptr).
The two of them are, so to speak: "a different pair of shoes".
What I've been referring to, is only: indexing a ZString (ZString Ptr = unchanged)

Apparently, you misunderstood my post to begin with. My example code applies to that post and further explains what I meant.
Last edited by Munair on Dec 06, 2018 22:09, edited 1 time in total.
Lost Zergling
Posts: 154
Joined: Dec 02, 2011 22:51
Location: France

Re: Get String Characters 4x Faster than Mid()

Postby Lost Zergling » Dec 06, 2018 21:04

@ Mr Swiss
"Swap just does, what it is expected to do (no more, no less):" => Yes thrue
Swap var_type1, var_type2 (var_type(n) means: any FB intrinsic data-type) => Yes thrue
the values held by any var_type(n) are simply exchanged ... => Fake ! this is just what it look likes but compiler behaves with ptr assignment as if name of var1 and name of var2 were permutted (not values). Consequences : Swap is not suited for memory swapping, nevertheless it might be usefull for something else.
result = var_type1 has now the value, previously held by var_type2 (and, vice-versa) => Yes thrue
I said "The purpose of swap in this context (in my mind) is to make a local variable "virtually static" in memory so as to minimize stack calls and/or backend dealloc/realloc. I see it as if using stack was a little bit like using processor cache...partially thrue I think"
=> What I wished to point out is not what the instruction itself is doing (just doing what it is expected to), but the usage of this instruction into the lze algo so as to fake a static allocation as if it were a dynamic one so as to get a static allocation speed.
fxm
Posts: 8516
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Get String Characters 4x Faster than Mid()

Postby fxm » Dec 06, 2018 21:37

No, variable names are not swapped:

Code: Select all

Dim As Integer I, J
Print @I, @J
Swap I, J
Print @I, @J

Sleep
MrSwiss
Posts: 2862
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Get String Characters 4x Faster than Mid()

Postby MrSwiss » Dec 06, 2018 21:42

Lost Zergling wrote:Fake ! => this is just what it look likes but compiler behaves with ptr assignment

The Ptr stuff, is required to: handle, any FB intrinsic data-type.
(no other reason, I can imagine)
Lost Zergling
Posts: 154
Joined: Dec 02, 2011 22:51
Location: France

Re: Get String Characters 4x Faster than Mid()

Postby Lost Zergling » Dec 07, 2018 11:46

I think values are exchanged byref, perhaps like a resident (?) translation. This allow to swap zstrings of different size neither than getting an horrible crash nor than getting a slowdown as if values had to be reallocated. When using Mid, memory managment is embedded. Swap look likes a pretty easy solution to optimize the zstring + pointers option versus Mid.
Lost Zergling
Posts: 154
Joined: Dec 02, 2011 22:51
Location: France

Re: Get String Characters 4x Faster than Mid()

Postby Lost Zergling » Dec 07, 2018 21:20

@fxm. I actually made a confusion: I assimilated the name of the variable as it is identified by the programmer to the address of the variable. From the programmer's point of view, everything happens as if var1 becomes var2 and vice versa but and as you pointed out, the addresses of var1 and var2 are not modified. If I swap on pointers, the values contained in the pointers are swapped but not the data pointed and everything happens as if ptr1 became ptr2 at the level of the identifiers in the source code but the content of the data is not impacted, it was the idea. Then, the algo of the swap is not visible at my level, and extrapolating, I came to apprehend the swap more like a syntax swap of the name of two variables in the source code than as a physical permutation of the data. in memory. I think that was probably a mistake, but it was nevertheless useful to see it as well.
fxm
Posts: 8516
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Get String Characters 4x Faster than Mid()

Postby fxm » Dec 07, 2018 22:26

If you have another reference for each of the two variables, you can not reason as a name permutation because it was as if the other alias names were also implicitly permuted:

Code: Select all

Dim As Integer I1 = 1, I2 = 2
Dim Byref As Integer RI1 = I1, RI2 = I2

Print I1, I2
Print RI1, RI2
Print

Swap I1, I2

Print I1, I2
Print RI1, RI2

Sleep
These really are the two contents that are permuted.
Munair
Posts: 802
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Get String Characters 4x Faster than Mid()

Postby Munair » Dec 08, 2018 6:12

fxm wrote:These really are the two contents that are permuted.

One wouldn't expect otherwise.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests