Code: Select all
AString.s = "Apple pie"
*ZString.String = @AString
Debug *ZString\s
Code: Select all
AString.s = "Apple pie"
*ZString.String = @AString
Debug *ZString\s
dunno, well, isn't *zstring pointing to the array descriptor instead of the string itself?Trond wrote:What is wrong with this code?Code: Select all
AString.s = "Apple pie" *ZString.String = @AString Debug *ZString\s
Code: Select all
AString.s = "Apple pie"
*ZString = @AString
Debug PeekS(*zstring+OffsetOf(string\s))
You find a significant speed advantage with a heavy workload as it's all direct memory access. (it's more impressive with more complicated structures for sure)But what has me curious is what is the advantage of using this at all?
I want to cast the address of a string to a string to avoid the speed penalty of a function call to PeekS().Dare2 wrote:But what has me curious is what is the advantage of using this at all?
Code: Select all
String.s = "Apple pie"
Address = @String
*StringPtr.s
!mov eax, [v_Address]
!mov [p_StringPtr], eax
Debug *StringPtr
If you can manage that I'd like to know how, but afaik you can't get something for nothing.avoid the speed penalty of a function call to PeekS()
Yes, but I'm just showing the generic way to get data from structures using pointers. Most real-world applications have more complicated structures.That is functionally the same as PeekS(*ZString) as the offset is 0
Code: Select all
AString.s = "Apple pie"
*ZString.String = @AString
Debug PeekS(*ZString) ; This should be the same as
Debug *ZString\s ; this, since the offset is 0.
; By using this structured pointer a lot of time can
; saved since we don't have to call PeekS(), which in
; turn most likely creates a temporary string buffer
; (it's not simply a cast).
; But, for some reason, the version without PeekS()
; doesn't work. I thought at first it was a bug, but
; I didn't want to say anything about that since I've
; always used PeekS() before and I'm not much into using
; pointers this way.
; Now I think it's a bug.
You can do it like this, but I thought that maybe there was a more elegant way using a structured pointer. Remember to declare the destination string as *pointer to a string, not a string, or you'll get som hard to trace memory leaks.netmaestro wrote:If you can manage that I'd like to know how, but afaik you can't get something for nothing.avoid the speed penalty of a function call to PeekS()
Code: Select all
PointerToString.l = @"Apple Pie"
*StringPtr.s
!mov eax, [v_PointerToString]
!mov [p_StringPtr], eax
Debug *StringPtr
Yes, but I'm just showing the generic way to get data from structures using pointers. Most real-world applications have more complicated structures.[/quote]I thought this structure was made exclusively for this very purpose, a speedup gain from avoiding PeekS(), so it would be as real-world as neccessary already?That is functionally the same as PeekS(*ZString) as the offset is 0
Code: Select all
Structure ComplexStruct
Val1.q
Val2.d
Val3.f
Val4.l
Val5.s
EndStructure
Procedure.l DoSthWithStruct(*PStruct.ComplexStruct)
Debug *PStruct\Val1
Debug *PStruct\Val2+1.4
Debug *PStruct\Val2 ;* *PStruct\Val3
Debug *PStruct\Val5
EndProcedure
MyStruct.ComplexStruct
With MyStruct
\Val1=1858485
\Val2=1.6
\Val3=2.0
\Val5="Hallo Welt!"
EndWith
DoSthWithStruct(@MyStruct)
Code: Select all
AString.s = "Apple pie"
ZString.String
ZString\s=AString.s
*PString.String=@ZString
Debug *PString\s
It does work, but it's not much faster than using PeekS() since you do a copy of the string when you do ZString\s=AString.s. I wanted to cast a long to a string, and don't copy it.Nik wrote:works too!Code: Select all
AString.s = "Apple pie" ZString.String ZString\s=AString.s *PString.String=@ZString Debug *PString\s
Definetely copying a pointer then accessing the string from the pointer would be faster than copying the entire string:Just wondering now if assigning addresses to the string structure and then accessing the string would be faster than PeekS. Can't really tell because can't test.
Code: Select all
AString.s = "Apple pie"
#Tries = 10000000
; Fast way of doing it (don't copy the string, just move the pointer)
time = GetTickCount_()
For I = 0 To #Tries
!mov edx, [v_AString]
!mov [p_InSane], edx
*InSane.s
Next
MessageRequester("", Str(GetTickCount_()-time))
; PeekS() way of doing it
time = GetTickCount_()
For I = 0 To #Tries
*InSane.s = PeekS(@AString)
Next
MessageRequester("", Str(GetTickCount_()-time))
Code: Select all
Structure tooMany
StructureUnion
Zstring.string
ptr.l
EndStructureUnion
EndStructure
test.tooMany
test\ptr = AllocateMemory(20)
PokeS(test\ptr,"Peaches and Pears")
Debug test\Zstring\s
FreeMemory(test\ptr)