AI says 'Caching values' in loops containing string.ToLowerCase improves performance, is this true?

Chris2

Active Member
Licensed User
Longtime User
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:

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 ...
This calls .ToLowerCase twice — each call creates a new string and performs character conversions.
✅ Instead:
B4X:
Dim lowerS As String = s.ToLowerCase
If lowerS.Contains("abc") And lowerS.StartsWith("xyz") Then ...
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....

3. Improves Loop Efficiency

In a loop with 10,000 strings:
B4X:
For Each s In list
    If s.ToLowerCase.Contains("abc") Then ...
Next
✅ With caching:
B4X:
For Each s In list
    Dim lowerS As String = s.ToLowerCase
    If lowerS.Contains("abc") Then ...
Next
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
B4X:
For Each s In list
    Dim lowerS As String = s.ToLowerCase
    If lowerS.Contains("abc") Then ...
Next
the declartion of lowerS happens only once?"

❌ 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.
This is much better than calling .ToLowerCase multiple times per iteration (like inside .Contains() and .StartsWith()).


So, is
B4X:
For Each s In list
    Dim lowerS As String = s.ToLowerCase
    If lowerS.Contains("abc") Then ...
Next
really better in terms of performance than
B4X:
For Each s In list
    If s.ToLowerCase.Contains("abc") Then ...
Next
?
 
Last edited:

emexes

Expert
Licensed User
Longtime User
What did your timing measurements say?

I doubt there is much in it, so I'd go with the most programmer-friendly rendition.

The two-line version might have a slight advantage when debugging.
 

emexes

Expert
Licensed User
Longtime User
FWIW I found the shorter ("uncached") loop to be 5.5% quicker than the "Dim LowerS" loop.

My only change to the "AI" code was to add "As String" to the For Each line.

Welcome to AI. It sounds like it knows what it's talking about, but it doesn't always.

Makes you wonder how correct it is when it's talking about something you're not familiar with.

There's a name for that effect, but it slips my mind at the moment.
 
Last edited:

Chris2

Active Member
Licensed User
Longtime User
B4X:
Private Sub LoopTest
    
    Dim list As List
    list.Initialize
    Dim i As Int
    
    For i=0 To 40000000    ' 'more loops gives me a java.lang.OutOfMemoryError: Java heap space
        list.Add(RandomString(20))
    Next
    
'    loop1(list)
    loop2(list)
End Sub

Private Sub loop1(list As List)
    Dim start, timetaken As Long
    start = DateTime.Now
    
    For Each s As String In list
        Dim lowerS As String = s.ToLowerCase
        If lowerS.Contains("99") Then
            'don't do anything
        End If
    Next
    
    timetaken = DateTime.Now - start
    Log(timetaken)
End Sub

Private Sub loop2(list As List)
    Dim start, timetaken As Long
    start = DateTime.Now
    
    For Each s As String In list
        If s.ToLowerCase.Contains("99") Then
            'don't do anything
        End If
    Next
    
    timetaken = DateTime.Now - start
    Log(timetaken)
End Sub

Private Sub RandomString(length As Int) As String
    Dim chars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    Dim sb As StringBuilder
    sb.Initialize

    For i = 1 To length
        Dim r As Int = Rnd(0, chars.Length)
        sb.Append(chars.CharAt(r))
    Next

    Return sb.ToString
End Sub

loop1 time taken
4223
4192
4227

loop2 time taken
4260
4147
4289

I doubt there is much in it
Correct!
 

emexes

Expert
Licensed User
Longtime User
There's a name for that effect, but it slips my mind at the moment.


Lol found by asking AI but it took two tries plus a name-dropping to get to the correct answer:


This effect is so true. Drives me nuts.

Briefly stated, the Gell-Mann Amnesia effect is as follows. You open the newspaper to an article on some subject you know well. In Murray's case, physics. In mine, show business. You read the article and see the journalist has absolutely no understanding of either the facts or the issues. Often, the article is so wrong it actually presents the story backward—reversing cause and effect. I call these the "wet streets cause rain" stories. Paper's full of them.

In any case, you read with exasperation or amusement the multiple errors in a story, and then turn the page to national or international affairs, and read as if the rest of the newspaper was somehow more accurate about Palestine than the baloney you just read. You turn the page, and forget what you know.


It was Trump talking about Fake News during 2016 election that truly opened my eyes to mainstream media bias and bs.

I've found that for a more useful, reliable, realistic and entertaining picture of what's happening in the world, this site is king:


1749731508230.png
1749732186761.png
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User

Lol found by asking AI but it took two tries plus a name-dropping to get to the correct answer:


This effect is so true. Drives me nuts.

Briefly stated, the Gell-Mann Amnesia effect is as follows. You open the newspaper to an article on some subject you know well. In Murray's case, physics. In mine, show business. You read the article and see the journalist has absolutely no understanding of either the facts or the issues. Often, the article is so wrong it actually presents the story backward—reversing cause and effect. I call these the "wet streets cause rain" stories. Paper's full of them.

In any case, you read with exasperation or amusement the multiple errors in a story, and then turn the page to national or international affairs, and read as if the rest of the newspaper was somehow more accurate about Palestine than the baloney you just read. You turn the page, and forget what you know.


It was Trump talking about Fake News during 2016 election that truly opened my eyes to mainstream media bias and bs.

I've found that for a more useful, reliable, realistic and entertaining picture of what's happening in the world, this site is king:


View attachment 164736

This is not completely true. If I do not know how to perform surgeries and I start to explain a lot of things about them saying balonies, it does not mean that I cannot program. It means that I need to get an experienced scientific advisor which will proof read my sayings to filter my balonies. F.e. during the pandemic I approached biology which I hated in school. Does this mean I am expert and no balonies will come out of my mouth? No, it means that when you know someone spilled oil on the floor, just go next to the wall and take small steps. Know your ignorance...
 
Last edited:

Chris2

Active Member
Licensed User
Longtime User
Well, my AI conversation wasn't a complete waste of time - it led me towards a conversation with a human being in which I learnt something!

Unless you are dealing with lists of millions of items then such (pre)optimizations are meaningless.
I've seen you say similar things a few times in the forum @Erel and it's definitely a rule that I stick to, if for no other reason than I'm aware that I don't know enough to be sure that one way is 'better' than another.
My query here was more about the logic of how adding an extra declaration inside the loop could make things quicker - the answer being that (at least in a B4J context) it doesn't!

Benchmarking such loops is also difficult as the Java compiler and VM are very sophisticated and can apply all kinds of optimizations.
It's still a valid benchmark test for how things perform specifically within a B4X app though, right? If the Java compiler and VM are optimising behind the scenes then that's fine, they'd do it in a released app too.
 
Last edited:
Top