Repeated strings and optimisation

Everything else that doesn't fall into one of the other PB categories.
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Repeated strings and optimisation

Post by BarryG »

My app has a lot of repeated hard-coded literal strings of "Done" throughout (about 60). Does PureBasic create a new internal string for all these occurrences, or does it make one single reference to it and call that every time "Done" is used in my code? In other words, should I perhaps set Global done$="Done" instead and used done$ throughout my code? Is that a better for optimisation? Or maybe as a constant?

I haven't used a Global variable for it yet because that makes it harder to see in my code (due to not being highlighted as a literal string anymore, which is important for me).
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Repeated strings and optimisation

Post by Josh »

For string literals and/or constants only 1x memory is allocated.

Code: Select all

#Name = "Barry"
PokeS (@"Barry", "BARRY")
Debug #Name
Ask 'Kurzer' how his balls are. He can tell you more about this topic :mrgreen:
sorry for my bad english
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Repeated strings and optimisation

Post by TI-994A »

BarryG wrote:My app has a lot of repeated hard-coded literal strings of "Done" throughout (about 60). Does PureBasic create a new internal string for all these occurrences, or does it make one single reference to it and call that every time "Done" is used in my code? In other words, should I perhaps set Global done$="Done" instead and used done$ throughout my code? Is that a better for optimisation? Or maybe as a constant? ...
This would be a choice based purely on performance and memory management.

Global variables are still variables, meaning that their values are mutable. The resources required to maintain references to global variables are locked throughout the life cycle of the program, as they must be accessible and modifiable from every stack, and only released upon program termination. Local variables, on the other hand, are instantiated and used only within the scope of the stack in which they are defined, and released once the stack is popped. Clearly more efficient, unless in cases of recursion.

Alternatively, for values that do not require mutation, constants might prove a better choice. Like global variables, constants are also locked throughout the life cycle of the program and accessible from anywhere in the program, but since they are non-mutable objects, they benefit from better compiler optimisations.

IMHO, for the requirement you've cited, it's never a good idea to redefine common values used throughout the program. So, constants would be the ideal implementation.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 666
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Repeated strings and optimisation

Post by Kurzer »

BarryG,

apart from the fact that using 60 hard coded "done"s in the source code is not a good programming style (you will notice this if you want to change something in the literal later and have to do it 60 times), the PB compiler converts this literal into a single string literal and refers to it at the 60 places.

As Josh has already pointed out, I learned this the hard way: viewtopic.php?p=550897#p550897

So, in your case, I would definitely use a constant for that.
#DONE_MSG = "Done"

Stay well!
Kurzer
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Repeated strings and optimisation

Post by Josh »

I don't know how it's implemented in your case. But if possible, you should always use integer variables:

Code: Select all

Structure MYSTRUC
  ...
  IsDone.b
  ...
EndStructure
sorry for my bad english
Post Reply