I've been having my first play with https://chatgpt.com/g/g-67c449c559188191877c2b94019f028b-b4x-assistent (thanks to @Alexander Stolte!).
Just firing questions and trying to learn.
I ended up chatting about string performace in relation to string.ToLowerCase and it started talking about "Caching Values".
The first bits I understood:
This calls .ToLowerCase twice — each call creates a new string and performs character conversions.
Instead:
Now .ToLowerCase is done once and reused — faster and less memory churn.
So far so good.
but then I didn't quite understand what it said next....
With caching:
You now compute it once per string, not multiple times — saving time and CPU cycles.
Which I didn't understand. To me it looks like I'm still doing s.ToLowerCase once per itteration (10,000 times) but now I'm adding the declaration of a variable each itteration which is surely more work (or so I thought)?
So I asked:
"So in
the declartion of lowerS happens only once?"
So, is
really better in terms of performance than
?
Just firing questions and trying to learn.
I ended up chatting about string performace in relation to string.ToLowerCase and it started talking about "Caching Values".
The first bits I understood:
What Does "Caching Values" Mean?
It means storing the result of a calculation or function call in a variable, so you can reuse it without repeating the computation.Why Caching Helps:
1. Avoids Repeated Computation
B4X:
If s.ToLowerCase.Contains("abc") And s.ToLowerCase.StartsWith("xyz") Then ...

B4X:
Dim lowerS As String = s.ToLowerCase
If lowerS.Contains("abc") And lowerS.StartsWith("xyz") Then ...
So far so good.
but then I didn't quite understand what it said next....
3. Improves Loop Efficiency
In a loop with 10,000 strings:
B4X:
For Each s In list
If s.ToLowerCase.Contains("abc") Then ...
Next

B4X:
For Each s In list
Dim lowerS As String = s.ToLowerCase
If lowerS.Contains("abc") Then ...
Next
Which I didn't understand. To me it looks like I'm still doing s.ToLowerCase once per itteration (10,000 times) but now I'm adding the declaration of a variable each itteration which is surely more work (or so I thought)?
So I asked:
"So in
B4X:
For Each s In list
Dim lowerS As String = s.ToLowerCase
If lowerS.Contains("abc") Then ...
Next
No, the declaration Dim lowerS As String = s.ToLowerCase is executed in every loop iteration, but...
Yes, the variable lowerS itself is allocated only once, and then reused with a new value each time.
What Really Happens:
B4X:
For Each s In list
Dim lowerS As String = s.ToLowerCase
If lowerS.Contains("abc") Then ...
Next
- On each iteration:
- s.ToLowerCase is called once.
- The result is stored in lowerS.
- lowerS is a new string reference each time, but the variable itself is reused.
So, is
B4X:
For Each s In list
Dim lowerS As String = s.ToLowerCase
If lowerS.Contains("abc") Then ...
Next
B4X:
For Each s In list
If s.ToLowerCase.Contains("abc") Then ...
Next
Last edited: