Page 1 of 1

What's the fastest way to compare two structures?

Posted: Sat Apr 30, 2022 11:44 am
by Caronte3D
I was found the below snippet in the forum, but since it uses json library, perhaps exists a faster way (maybe comparing memory positions directly?).
What do you think?

Code: Select all

  Macro CompareStructure(data1, data2, struc, result)
    Define json1, json2
    json1 = CreateJSON(#PB_Any)
    json2 = CreateJSON(#PB_Any)
    If json1 And json2
      InsertJSONStructure(JSONValue(json1), data1, struc)
      InsertJSONStructure(JSONValue(json2), data2, struc)
      PokeI(@result, Bool(ComposeJSON(json1) = ComposeJSON(json2)))
    EndIf
    If json1 : FreeJSON(json1) : EndIf
    If json2 : FreeJSON(json2) : EndIf
  EndMacro

Re: What's the fastest way to compare two structures?

Posted: Sat Apr 30, 2022 12:29 pm
by mk-soft
Only base var types without strings and pointers ...

Code: Select all

Structure udtMyData
  iVal.i
  fltVal.f
EndStructure

Global d1.udtMyData, d2.udtMyData

Debug SizeOf(udtMyData)

If CompareMemory(d1, d2, SizeOf(udtMyData))
  Debug "ok"
Else
  Debug "failed"
EndIf


d1\iVal = 1

If CompareMemory(d1, d2, SizeOf(udtMyData))
  Debug "ok"
Else
  Debug "failed"
EndIf

Re: What's the fastest way to compare two structures?

Posted: Sat Apr 30, 2022 1:48 pm
by Caronte3D
Ugh! :? I have strings.
Maybe I can use this method often and check the strings less often.
Thanks mk-soft :wink: