What's the fastest way to compare two structures?

Advanced game related topics
User avatar
Caronte3D
Addict
Addict
Posts: 1055
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

What's the fastest way to compare two structures?

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 5405
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Caronte3D
Addict
Addict
Posts: 1055
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

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

Post by Caronte3D »

Ugh! :? I have strings.
Maybe I can use this method often and check the strings less often.
Thanks mk-soft :wink:
Post Reply