Optimizing program's speed

BlitzMax Forums/BlitzMax Programming/Optimizing program's speed

splinux(Posted 2006) [#1]
Which from this(in order) is the faster method to store and read data?
-arrays
-banks
-variables
-other?

I think is so: banks-variables-arrays(left to right, faster to slower).
Is it correct?
Any ideas on how to optimize data storing speed?


Drey(Posted 2006) [#2]
Local I,C,A[5]
ME = 1000 ' millisec end time

Rem 
Basic cut and paste model

T = MilliSecs()
TE = T + ME
Repeat 
C:+1


Until TE <= MilliSecs()
A[i] = C
Print C
I:+1
C=0

End Rem 
Local X,Y

T = MilliSecs()
TE = T + ME
Repeat 
C:+1
	
	X = Y

Until TE <= MilliSecs()
A[i] = C
Print C
I:+1
C=0


Local Ar[2]
T = MilliSecs()
TE = T + ME
Repeat 
C:+1
	

	Ar[1] = Ar[0]

Until TE <= MilliSecs()
A[i] = C
Print C
I:+1
C=0

B = CreateBank(12)

T = MilliSecs()
TE = T + ME
Repeat 
C:+1

	PokeInt(B,1,PeekInt(B, 0))


Until TE <= MilliSecs()
A[i] = C
Print C
I:+1
C=0


Variables, Arrays...banks. I really don't get the purpose of banks yet..maybe i'm using them wrong.


ImaginaryHuman(Posted 2006) [#3]
I would've said variables are fastest because they are direct with no offsets. Then arrays because you have one level of indirectness with the offset. Then banks because you have two levels of indirectness - finding the bank base based on the bank handle, and then adding the offset.

If you can use it, using EachIn can be a bit faster than a for/next loop, at least I find it to be. Others have said the opposite.

Banks are more appropriate to store larger amounts of data than to think of it as a big array. Also banks can be loaded/saved to disk/streamed.

You can treat a bank like an array to make it faster:

Local MyBank:TBank=CreateBank(1024)
Local MyBuf:Byte Ptr=BankBuf(MyBank)
MyBuf[0]=15 ' treat it like an array
Print MyBuf[5] ' treat it like an array

(or use an Int Ptr, for example).

By KEEPING the pointer to the bank's base address, you avoid having to SELECT which bank to work with, which happens every time you do a Poke or Peek. I'm using banks to store custom bitmaps formats.

P.S. You can expect to hear someone tell you not to worry about optimizing anything because today's computers are make it unnecessary. ;-D


Drey(Posted 2006) [#4]
@Angel Yeah, that last line is filth. People just stop trying. They use the first solution that comes to mind and they're done. Mess.

Why would you have to use a bank instead of an array though? Saved to a disk? Can you explain before i get deeper into the engine.


ImaginaryHuman(Posted 2006) [#5]
What difference is a variable to an array to a bank? They're all storage spaces, thought of differently. As is a pixmap, or a list, or whatever else.

They have their uses, and their uses often overlap.

You can save a bank to disk as a file, and also load it in from a file. You can't load into an array, without some further steps.

A bank is an array with extra information attached to it I guess.

And hey, even if they're all similar, you get to choose, which is the good part.


CS_TBL(Posted 2006) [#6]
A bank is just a chunk o' memory, at your service. In non-max they could be used for variable-storage, if you want to keep variables local and don't want to work with types. I did whole programs (all variables needed for it) and datastructures in banks in B+, but with BMax types will do for me. :P

Banks are nice to load a chunk o' binary data which you could inspect/split later into your own variables, like a fileformat.


ImaginaryHuman(Posted 2006) [#7]
You also might prefer using a bank if you're manipulating a large chunk of memory containing some kind of data - bitmap data, coordinates, whatever. It's more of a mindset. I think arrays have some taboo with regards to using them for `big tasks`.


Drey(Posted 2006) [#8]
hmm..well, i'm glad the option is there too.


splinux(Posted 2006) [#9]
Ok. thanks.


SebHoll(Posted 2006) [#10]
Just as an example, I use banks a lot and wouldn't know what to use without them. They are REALLY useful! My programs involve reading Image/Audio/Movie files, changing the contents of them bit by bit and then saving them. Sometimes they can be 50MB and can be quickly read/saved from/to the HDD. Not really much point in them though if you aren't doing file/network operations.

Seb


Defoc8(Posted 2006) [#11]
If i create a block of memory + format it..say to hold image
data...to transfer that block to another area of memory,
say on the gfx card...all that needs to be done is a memcpy
operation..or if copying to system memory, sometimes a
pointer swap will do the trick - the memory has been
formatted...- direct mapping simpilfies things.
If i were to use an array, and if that array was
multi-dimensional + stored struct data..there is no way to
know how this is layed out in memory..atleast you cannot
be 100% sure - so the copy operation is complicated..

It really depends on the situation -...but i would avoid banks
unless your doing a lot of large memory based processing
operations...meh..its your call ;)