Type vs Array vs Bank to store datas and space tak

Blitz3D Forums/Blitz3D Beginners Area/Type vs Array vs Bank to store datas and space tak

RemiD(Posted 2013) [#1]
Hello, :)

I am doing some tests with types and dim arrays and banks to store datas and see how much space it takes in memory.

I want to store 100x100 heights (from 0 to 255) of 100x100 terrains.

Firstly, can you please tell me if this code is correct :




Secondly, can you please tell me if my understanding of the space taken in memory is correct :

With types, i can't store a byte, so i have to store an integer, so the space taken in memory will be :
4x100x100x100x100 = 400 000 000 or 400mb

With dim arrays, i can't store a byte, so i have to store an integer, so the space taken in memory will be :
4x100x100x100x100 = 400 000 000 or 400mb

With banks, i can store a byte, so the space taken in memory will be :
1x100x100x100x100 = 100 000 000 or 100mb



Thanks, good day


_PJ_(Posted 2013) [#2]
You will have to be very careful with the memory management if you really intend to use this much in one go!

Identifying timing in this manner isn't always reliable, since the CPU may be busy with other processes, the memory may be more or less full, and of course, is largely system-dependent.

Your memory assumptions are correct, although bear in mind that Type objects may contain a little more information, as each object will have its own identity.

Typically, banks should always be much faster, since they operate directly with the memory heap and do not require excess translation - however, manipulating memory may take a modicum of extra calculations but on most systems nowadays that shouldn't be an issue.

;DECLS
Const TERRAINS=10000
Const HEIGHTS=10000

Dim ARRAYS(0,0)

Type TYPES
	Field HEIGHTS
End Type

Global BANKS;=CreateBank(TERRAINS*HEIGHTS)

;EXAMPLE

Print Str(ArrayWriteTime())+" ms to populate integers in Array"
Print Str(ArrayReadTime())+" ms to retrieve integers from Array"
Print Str(TypeWriteTime())+" ms to populate integers in Type"
Print Str(TypeReadTime())+" ms to retrieve integers from Type"
Print Str(BankWriteTime())+" ms to populate bytes in Bank"
Print Str(BankReadTime())+" ms to retrieve bytes from Bank"
WaitKey()
End






;FUNCTIONS

Function ArrayWriteTime%()
	;Reserve Memory
	Dim ARRAYS(TERRAINS,HEIGHTS)
	
	Local Iter1
	Local Iter2
	Local Start=MilliSecs()
	
	For Iter1 = 0 To HEIGHTS-1
		For Iter2 = 0 To TERRAINS-1
			ARRAYS(Iter2,Iter1)=Rand(0,255)
			;DebugLog "Arrays: "+Str((HEIGHTS*TERRAINS-1)-((Iter1*TERRAINS)+Iter2))
		Next
	Next
	
	Local Finish=MilliSecs()-Start
	
	Return Finish
End Function

Function ArrayReadTime%()
	Local Iter1
	Local Iter2
	Local Test
	
	Local Start=MilliSecs()
	
	For Iter1 = 0 To HEIGHTS-1
		For Iter2 = 0 To TERRAINS-1
			Test=ARRAYS(Iter2,Iter1)
			;DebugLog "Arrays: "+Str((HEIGHTS*TERRAINS-1)-((Iter1*TERRAINS)+Iter2))
		Next
	Next
	
	Local Finish=MilliSecs()-Start
	
	;Free Memory
	Dim ARRAYS(0,0)
	
	Return Finish
End Function

Function TypeWriteTime%()
	Local Iter
	Local Max=(TERRAINS*HEIGHTS)-1
	Local T.TYPES
	
	Local Start=MilliSecs()
	
	For Iter = 0 To Max
		;Reserve Memory
		T.TYPES=New TYPES
		T\HEIGHTS=Rand(0,255)
		;DebugLog "Types: "+Str(Max-Iter)
	Next
	
	Local Finish=MilliSecs()-Start
	
	Return Finish
End Function

Function TypeReadTime%()
	Local Max=(TERRAINS*HEIGHTS)-1
	Local T.TYPES
	Local Iter
	Local Test
	
	Local Start=MilliSecs()
	
	For T.TYPES=Each TYPES
		Test=T\HEIGHTS
		Iter=Iter+1
		;DebugLog "Types: "+Str(Max-Iter)
	Next
	
	Local Finish=MilliSecs()-Start
	
	;Free Memory
	Delete Each TYPES
	
	Return Finish
End Function

Function BankWriteTime%()
	;Reserve Memory
	BANKS=CreateBank(TERRAINS*HEIGHTS)
	
	Local Iter
	Local Max=(TERRAINS*HEIGHTS)-1
	
	Local Start=MilliSecs()
	
	For Iter = 0 To Max
		PokeByte BANKS,Iter,Rand(0,255)
		;DebugLog "Banks: "+Str(Max-Iter)
	Next
	
	Local Finish=MilliSecs()-Start
	
	Return Finish
End Function

Function BankReadTime%()
	Local Iter
	Local Max=BankSize(BANKS)-1
	Local Test
	
	Local Start=MilliSecs()
	
	For Iter = 0 To Max
		Test=PeekByte(BANKS,Iter)
		;DebugLog "Banks: "+Str(Max-Iter)
	Next
	
	Local Finish=MilliSecs()-Start
	
	;Free Memory
	FreeBank BANKS
	
	Return Finish
End Function



RemiD(Posted 2013) [#3]
_PJ_>>Thanks for your feedback


You will have to be very careful with the memory management if you really intend to use this much in one go!

Identifying timing in this manner isn't always reliable, since the CPU may be busy with other processes, the memory may be more or less full, and of course, is largely system-dependent.


What do you mean by that ? I thought that i was able to create a bank when i wanted if the target computer has enough free memory space.

For example, if the program requires to have at least a computer with 512mo of free memory space, and asks the user to not run others programs at the same time, i thought that i could use almost all of this free memory space to store the datas for the game.
If i can't assume that there is enough free memory space, is there a way to check how much free memory space there is ?

Thanks,


RemiD(Posted 2013) [#4]

is there a way to check how much free memory space there is ?


Apparently yes :
http://www.blitzbasic.com/Community/posts.php?topic=39105 (#6)

Edit : it does not seems to return the correct values, i will have to search more.


_PJ_(Posted 2013) [#5]
I wonder if incorrect values are due to having >4Gig of RAM, since Blitz is limited to 32-Bit integers (Signed ones at that, so over 16 777 216 Megabytes might be reported oddly?)