Banks vs Arrays

Blitz3D Forums/Blitz3D Programming/Banks vs Arrays

Are arrays faster than banks? I've always thought otherwise, but while making my own bank library, I decided to test it.

Graphics 640,480,0,2

SeedRnd MilliSecs()

;Bank
Bank = CreateBank(100000 * 4)

;Array
Dim Array(100000)


;Bank-test
Start = MilliSecs()
	For x = 1 To 1000

		For i = 1 To 100000
			PokeFloat(Bank, (i-1)*4, Rnd(0.0, 100000.0))
		Next
	
		;For i = 0 To 399999 Step 4
		;	PokeFloat(Bank, i, Rnd(0.0, 100000.0))
		;Next
	
		For i = 1 To 100000
			Temp = PeekFloat(Bank, (i-1)*4)
		Next

		;For i = 0 To 399999 Step 4
		;	Temp = PeekFloat(Bank, i)
		;Next

	Next

Time = MilliSecs() - Start


;Array-test
Start = MilliSecs()
	For x = 1 To 1000

		;Dim Array(100000)

		For i = 1 To 100000
			Array(i) = Rnd(0.0, 100000.0)
		Next

		For i = 1 To 100000
			Temp = Array(i)
		Next

	Next

Time2 = MilliSecs() - Start

Print "Bank: Time " + Time + " ms"
Print "Dim:  Time " + Time2 + " ms"
WaitKey


I get around 6500ms for bank and around 5000-5300ms for array. Have I made some mistake in the test-code?

The commented out ways of calculating the position within the bank don't affect the results much. Array is faster even if it is re-dimmed every 'x'-loop

Arrays are going to be faster because you don't have to use a go-between function to get at the data (in a sense).

I myself don't use arrays or banks when I can avoid them, I prefer to use stacks. (My stack code)

i like arrays - they keep things simple for me. then comes along noel with his stack code to slap my brains around a bit :P

I quite like banks actually; they may be slightly slower than arrays depending on your usage, but the fact that you can slam a bank handle into a type instance field means you can store a shedload of local variables in a 100% flexible format per instance without having to predefine anything other than the header format at design time.

Pretty much the only thing I still use arrays for are tiled maps, and even then I've started to use banks for those instead (especially on certain i/o routines, like grabbing from dlls or reading from the filesystem).

I like banks when it comes to compressing data during run-time. For example, my game has a heck of a lot of data which would hog the memory like crazy if left in array form. By reducing it to mere bit patterns and only using the bare minimum amount of bits, I can cram a lot more in.