Have a look (Types vs. Arrays)

BlitzMax Forums/BlitzMax Programming/Have a look (Types vs. Arrays)

Hi,

I'm working on a particle-sys atm, just to get comfort with BMax and OGL. I asked me what's faster to use: Types within Types (i.e. Field pos:TVector3) or Arrays (i.e. Field pos#[3]).

So I wrote a test-app with interesting results:
Types are faster to create, faster accessible and use less memory. Not what I expected.

Would you mind to run this for yourself, maybe I got an error in my code.

Strict

Type TFloat3
	Field f1#,f2#,f3#
	Function Create:TFloat3(v1#,v2#,v3#)
		Local me:TFloat3=New TFloat3
		me.f1=v1
		me.f2=v2
		me.f3=v3
		Return (me)
	End Function
End Type

Type ptype
	Field f:TFloat3
	Function Create:ptype(v1#,v2#,v3#)
		Local me:ptype=New ptype
			me.f=TFloat3.Create(v1,v2,v3)
		Return (me)
	End Function
End Type

Type parray
	Field f#[3]
	Function Create:parray(v1#,v2#,v3#)
		Local me:parray=New parray
		me.f[0]=v1
		me.f[1]=v2
		me.f[2]=v3
		Return (me)
	End Function
End Type

Local in$=Input ("How many elements?")
If in="" Then in="500000"
Local numObj:Int=Int(in)

Global atypes:ptype[numObj]
Global aarray:parray[numObj]


Global memory:Int
Global time:Int
Global n:Int
Global f1#,f2#,f3#

'wait some time
Print "please wait..."
Delay(5000)

'create and fill array
memory=GCMemAlloced()
time=MilliSecs()
For n:Int=0 To numObj-1
	aarray[n]=parray.Create(1.0,2.0,3.0)
Next
Print "Creating "+numObj+" parrays: "+(MilliSecs()-time)
Print "used memory: "+(GCMemAlloced()-memory)

'write array
time=MilliSecs()
For n=0 To numobj-1
	aarray[n].f=[1.0,2.0,3.0]
Next
Print "Write to "+numObj+" parrays: "+(MilliSecs()-time)

'Read array
time=MilliSecs()
For n=0 To numobj-1
	f1=aarray[n].f[0]
	f2=aarray[n].f[1]
	f3=aarray[n].f[2]
Next
Print "Read from "+numObj+" parrays: "+(MilliSecs()-time)


'create and fill types
memory=GCMemAlloced()
time=MilliSecs()
For n:Int=0 To numObj-1
	atypes[n]=ptype.Create(1.0,2.0,3.0)
Next
Print "-----------------"
Print "Creating "+numObj+" ptypes: "+(MilliSecs()-time)
Print "used memory: "+(GCMemAlloced()-memory)

'write  types
time=MilliSecs()
For n=0 To numObj-1
	atypes[n].f.f1=1.0
	atypes[n].f.f2=2.0
	atypes[n].f.f2=3.0
Next
Print "Write to "+numObj+" ptypes: "+(MilliSecs()-time)

'read types
time=MilliSecs()
For n=0 To numObj-1
	f1=atypes[n].f.f1
	f2=atypes[n].f.f2
	f3=atypes[n].f.f3
Next
Print "Read from "+numObj+" ptypes: "+(MilliSecs()-time)

End