Structure Optimization

Just starting out? Need help? Post your questions and find answers here.
Moonshine
Enthusiast
Enthusiast
Posts: 263
Joined: Tue May 25, 2004 12:13 am
Location: UK

Structure Optimization

Post by Moonshine »

I was just wondering if there are any advantages to static arrays or standard vars in a structure. For example:

Code: Select all

Structure Vector
x.f
y.f
z.f
EndStructure

Structure Vector
Position.f[3]
EndStructure
Is there any differnce/advantage to either of the methods, in terms of speed or memory management or anything like that. Just something that Id been wondering for a while.

Cheers :)
Mark my words, when you least expect it, your uppance will come...
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I wouldn't of thought so, because both ways places the data in exactly the same configuration, they are all concurrent positions in memory. A static array just provides another (more convienient?) way to set/get the data. :) I would personally go with the first more readable way of doing things. i.e.:

Code: Select all

Structure VECTOR
    x.f
    y.f
    z.f
EndStructure
--Kale

Image
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

There will be differences in the generated ASM code. Using an array the accesses always seem to be based on "array address + sizeof(type) * offset" whereas with the three separate variables it will only be "address of structure + offset". Whether this makes much difference I don't know, but array accesses certainly have the edge when it comes to ease of batch processing (in terms of writing the code :).

Edit: I just checked, this example show that the same code is generated for the simple case where you use a constant offset is the same, but when using expressions as the loop index is more complex. Of course, you can't use that kind of accesses with three separate variables, so you always get the simpler code.

Code: Select all

; Structure separate
; x.f
; y.f
; z.f
; EndStructure
; 
; 
; Structure array
; coords.f[3]
; EndStructure
; 
; 
; aa.separate
; bb.array
; 
; aa\x = 23
  LEA    ebp,[v_aa]
  MOV    dword [ebp],1102577664
; aa\y = 45
  MOV    dword [ebp+4],1110704128
; aa\z = 67
  MOV    dword [ebp+8],1116078080
; 
; bb\coords[0] = 12
  LEA    ebp,[v_bb]
  MOV    dword [ebp],1094713344
; bb\coords[1] = 23
  MOV    dword [ebp+4],1102577664
; bb\coords[2] = 34
  MOV    dword [ebp+8],1107820544
; 
; For i=0 To 2
  MOV    dword [v_i],0
_For1:
  MOV    eax,2
  CMP    eax,dword [v_i]
  JL    _Next2
; bb\coords[i] = 69
  LEA    ebp,[v_bb]
  PUSH   ebp
  MOV    eax,dword [v_i]
  SAL    eax,2
  POP    ebp
  ADD    ebp,eax
  MOV    dword [ebp],1116340224
; Next
_NextContinue2:
  INC    dword [v_i]
  JMP   _For1
_Next2:
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Post Reply