Alessandro71 Well-Known Member Licensed User Longtime User Aug 21, 2024 #1 Having a lot of string building statement, I was asking myself what is the best way to handle that, from a performance and memory handling perspective: B4X: Copy newstring = "constant string" & variablestring or B4X: Copy newstring = $"constant string${variablestring}"$ both produce the same result, but which one generates the best code and runtime performance?
Having a lot of string building statement, I was asking myself what is the best way to handle that, from a performance and memory handling perspective: B4X: Copy newstring = "constant string" & variablestring or B4X: Copy newstring = $"constant string${variablestring}"$ both produce the same result, but which one generates the best code and runtime performance?
Solution Daestrum Aug 21, 2024 I would guess the & version - it produces less java code Code: Copy ' & _newstring1 = "constant string"+_variablestring; ' smartstring _newstring2 = ("constant string"+anywheresoftware.b4a.keywords.Common.SmartStringFormatter("",(Object)(_variablestring))+"");
I would guess the & version - it produces less java code Code: Copy ' & _newstring1 = "constant string"+_variablestring; ' smartstring _newstring2 = ("constant string"+anywheresoftware.b4a.keywords.Common.SmartStringFormatter("",(Object)(_variablestring))+"");
Daestrum Expert Licensed User Longtime User Aug 21, 2024 #2 I would guess the & version - it produces less java code Code: Copy ' & _newstring1 = "constant string"+_variablestring; ' smartstring _newstring2 = ("constant string"+anywheresoftware.b4a.keywords.Common.SmartStringFormatter("",(Object)(_variablestring))+""); Upvote 0 Solution
I would guess the & version - it produces less java code Code: Copy ' & _newstring1 = "constant string"+_variablestring; ' smartstring _newstring2 = ("constant string"+anywheresoftware.b4a.keywords.Common.SmartStringFormatter("",(Object)(_variablestring))+"");
Sagenut Expert Licensed User Longtime User Aug 21, 2024 #3 Not sure about what I am about to write, but... Regular string are Immutable. So trying to modify it will, with a concatenation with & for example, anyway create an additional new string. To avoid this it should be used the StringBuilder that will manage mutable string and will effectively reuse the same string. Here the documentation: https://www.b4x.com/android/help/core.html#stringbuilder Upvote 0
Not sure about what I am about to write, but... Regular string are Immutable. So trying to modify it will, with a concatenation with & for example, anyway create an additional new string. To avoid this it should be used the StringBuilder that will manage mutable string and will effectively reuse the same string. Here the documentation: https://www.b4x.com/android/help/core.html#stringbuilder
Alessandro71 Well-Known Member Licensed User Longtime User Aug 21, 2024 #4 yes, but my use case is not about changing an existing string, rather than creating a new one from exiting ones Upvote 0
yes, but my use case is not about changing an existing string, rather than creating a new one from exiting ones
E emexes Expert Licensed User Longtime User Aug 22, 2024 #5 Alessandro71 said: Having a lot of string building statement Click to expand... Alessandro71 said: my use case is ... creating a new one from existing ones Click to expand... Why not StringBuilder? ("especially useful for when you need to concatenate many strings") Upvote 0
Alessandro71 said: Having a lot of string building statement Click to expand... Alessandro71 said: my use case is ... creating a new one from existing ones Click to expand... Why not StringBuilder? ("especially useful for when you need to concatenate many strings")
Erel B4X founder Staff member Licensed User Longtime User Aug 22, 2024 #6 Alessandro71 said: both produce the same result, but which one generates the best code and runtime performance? Click to expand... The performance difference, if there is one, is negligible = no user will ever feel any difference. Use the one that you feel that is simpler. If you are building very large strings then you should use StringBuilder. Upvote 0
Alessandro71 said: both produce the same result, but which one generates the best code and runtime performance? Click to expand... The performance difference, if there is one, is negligible = no user will ever feel any difference. Use the one that you feel that is simpler. If you are building very large strings then you should use StringBuilder.