Monday, November 5, 2012

Petit Computer Journal #12


Packing, Saving, and Loading Data

One of the most desirable feature of computer programming is data loading and saving. Nintendo DS is used mainly for gaming, and the ability to save and load large amount of data is mostly an afterthought. However, there is a built-in feature to do so: MEM$. Basically, we simply assign the the string to MEM$, and save that.

We can enter multiple data into MEM$ by using splitting numbers technique: MID$. Let's say that we put the data for score in MEM$ location 10-15. We can extract that by using MID$, like this:

SCORE=MID$(MEM$,10,5)

A similar technique is to pack numbers into one large number. You can split them out like so:

CLS
?"FOR BITS:"
?"DEC:1=10;2=100;3=1000"
?"BIN:1=2;2=4;3=8;4=16"
INPUT "NUMBER,BITS";N,B
@LOOP
?N%B
N=FLOOR(N/B)
IF N!=0 GOTO @LOOP
?"DONE!"

SAMPLE OUTPUT:
123456,100
56
34
12


Let's say that we have data in multiple column format, that we want to save. I'm going to make it easy for testing, and just use 192*16*16. First, we make the data, then we save it two ways: Graphic and MEM$. We also load it 2 ways. Finally, we're going to put some timer on each operation.

'Make up data
DIM D[50000]
FOR X=0 TO 191
FOR Y=0 TO 15
FOR Z=0 TO 15
D[X*256+Y*16+Z]=RND(256)
NEXT:NEXT:NEXT

'SAVE DATA GRAPH
T0=MAINCNTL
FOR X=0 TO 191
FOR Y=0 TO 15
FOR Z=0 TO 15
GPSET (Y*16+Z),X,D[X*256+Y*16+Z]
NEXT:NEXT:NEXT
?"SAVE GRP0:TEST1"
T1=MAINCNTL

'SAVE DATA MEM$
T2=MAINCNTL
FOR X=0 TO 191
MEM$=""
FOR Y=0 TO 15
FOR Z=0 TO 15
MEM$=MEM$+CHR$(D[X*256+Y*16+Z])
NEXT:NEXT
S$="MEM:MEM"+RIGHT$("000"+STR$(X),3):?"SAVE ";S$
NEXT
T3=MAINCNTL

?"GRAPH:",T1-T0
?"MEM$: ",T3-T2

However way I look at it, the MEM$ version is harder, longer, and slower. I purposely skip the actual saving process because it asks whether or not you want to overwrite existing files, which in MEM$ case, rather ponderous. Even so, GRAPH method is faster than MEM$

GRAPH:  300
MEM$:   632

Now let's do LOAD:

'LOAD DATA GRAPH
T0=MAINCNTL
?"LOAD GRP0:TEST1"
FOR X=0 TO 191
FOR Y=0 TO 15
FOR Z=0 TO 15
D[X*256+Y*16+Z]=GSPOIT(Y*16+Z,x)
NEXT:NEXT:NEXT
T1=MAINCNTL

'LOAD DATA MEM$
MEM$="A"*256
T2=MAINCNTL
FOR X=0 TO 191
S$="MEM:MEM"+RIGHT$("000"+STR$(X),3):?"LOAD ";S$
FOR Y=0 TO 15
FOR Z=0 TO 15
D[X*256+Y*16+Z]=VAL(MID$(MEM$,Y*16+Z,1))
NEXT:NEXT:NEXT
T3=MAINCNTL

?"GRAPH:",T1-T0
?"MEM$: ",T3-T2

And the result?

GRAPH:  266
MEM$:   840

In all cases, the Graph version is leaner, cleaner, and faster than the use of multiple MEM$.


1 comment:

programmerpro said...

please make more!