LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Reducing the overhead of strings in dictionaries

Root / Programming Questions / [.]

kldck_hulCreated:
Hi all, I wrote a language extension that looks up a string in a fake dictionary. When users are using this, it seems like an awful lot of garbage strings are created:
DEF update
  clamp "X","W"
  clamp "Y","H"
END
X,Y,W,H are all keys in a dictionary. Would it be beneficial to create an X$, Y$, W$, and H$ at the beginning of the program to avoid creating strings for the dictionary keys a lot? EDIT: I've noticed that SPANIM takes a string argument a lot like my dictionaries can:
SPANIM Management number,"Animation target",... Animation target Numerical value or character string to control the elements that should change
Is it common to see the target as a string? Is it an optimization to change these to a number?

This is a question you can answer yourself. Make a FOR loop that runs for 100000 iterations, doing it the constant way. And then another loop for the same number of iteration, doing it the variable way, and see which one takes longer.

Yeah probably. Sometimes the wiki has random notes like: - Its faster to write "" than to use a variable And I was kinda hoping someone would chime in with an already known axiom. I'll definitely write a test now since it doesn't seem like I'm ringing any bells.

You might be the first to test this in SmileBASIC. Please post your results!

I tested this a few times while at work today, running through a couple million iterations with different strings. Every test varied by at most 6 frames, which was about a 0.3% difference. Using a variable was unanimously "slower". I wonder if strings are created from arrays the same as they are from literals, which means the extra frames are time spent looking up the memory reference. I found that "" is actually slower than using a variable however. I wonder if this is because an array isn't allocated every time. Let me know if my findings are way different than someone else found. The test varied by so little, I'm wondering if theres some optimization taking place that I'm not aware of. I've been told smilebasic doesn't have constant strings.