It is currently Mon Apr 06, 2020 10:27 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Repeated strings and optimisation
PostPosted: Sat Mar 21, 2020 2:27 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 668
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).


Top
 Profile  
Reply with quote  
 Post subject: Re: Repeated strings and optimisation
PostPosted: Sat Mar 21, 2020 4:34 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1037
For string literals and/or constants only 1x memory is allocated.
Code:
#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


Top
 Profile  
Reply with quote  
 Post subject: Re: Repeated strings and optimisation
PostPosted: Sat Mar 21, 2020 5:54 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2305
Location: Singapore
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Repeated strings and optimisation
PostPosted: Sat Mar 21, 2020 1:32 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 11, 2006 12:07 am
Posts: 554
Location: Near Hamburg | Status: Currently not active in programming.
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 5.71 x64, OS: Windows 7 Pro x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age: 51y
"Happiness is a pet." | "Never run a changing system!"


Top
 Profile  
Reply with quote  
 Post subject: Re: Repeated strings and optimisation
PostPosted: Sat Mar 21, 2020 2:33 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1037
I don't know how it's implemented in your case. But if possible, you should always use integer variables:
Code:
Structure MYSTRUC
  ...
  IsDone.b
  ...
EndStructure

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Saki and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye