Ask coding questions

← Back to all posts
[PG-Basic] Which is more memory efficient: strings or arrays?
SixBeeps (1993)

Let's say we have a Basic program that has two variables, one of them is a string and the other is an array of strings. They both have the same contents. For example,

LET A="TEST"

ARRAY B
B[0]="T"
B[1]="E"
B[2]="S"
B[3]="T"

Ignoring the fact that making an array takes more code, which one would take up more memory (in RAM) to store? Which one is more "efficient"?

Commentshotnewtop
tharlynnhtetong (42)

If you're talking about just 1 string, then the variable would be more efficient because you won't have to recall 4 data slots and combine them together. Arrays are only more efficient when you have multiple strings you need to store, like a list of people's name.

SixBeeps (1993)

you won't have to recall 4 data slots

That's not the question though. My question is which one of them is more memory-efficient?

tharlynnhtetong (42)

@SixBeeps
If you're talking about memory then the answer is still the variable. An array will require more addresses than a string.

mwilki7 (943)

how are strings and arrays implemented in basic?

SixBeeps (1993)

@mwilki7 I looked at the source code and it looks like arrays are implemented as some kind of class called BasicArray. Strings are just strings in JS.

DynamicSquid (1134)

Stack Overflow is your friend here

SixBeeps (1993)

@DynamicSquid SO has nothing on PG-Basic

HenryZelonka1 (23)

I am not an expert on BASIC, but I believe all strings are handled as arrays of characters. Therefore, "TEST" and ["T","E","S","T"] would take the same amount of space. I would go with the option that involves less code, as that increases readability.

SixBeeps (1993)

@HenryZelonka1 That's what I'd imagine, but I just don't know if there are extra hidden "weights" involved with either one.