[Gambas-user] Speaking of optimizations

Tobias Boege taboege at gmail.com
Sun Mar 17 19:10:21 CET 2019


On Sun, 17 Mar 2019, Cedron Dawg wrote:
> I just found out about the .Max property of arrays, saving having to do a .Count - 1.  
> 
> I like it, but the name is a little confusing to me, I thought it would mean MaxValue, not MaxIndex.  No I can't think of a better short word.  "Top" is misleading, so is "Limit".
> 
> My real question is a performance related one.  Given:
> 
>         Dim a As New String[100]
>         Dim i, m As Integer
>         .
>         .        
>         m = a.Max
>         For i = 0 To m
>           ...
>         Next
>         .
>         .        
>         For i = 0 To a.Max
>           ...
>         Next
>         
> Does the first loop give any performance advantage over the second one?  Or does the compiler recognize that "a" won't change size in the loop and implement the first form under the covers?
> 

They are the same. The "For To" loop does even less than what you expect
it to: the To part is only once at the beginning:

  Public Sub Main()
    For i As Integer = 0 To i + 1
      Print i
    Next
  End

  > 0
  > 1

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list