Wishlist is to allow step to allow a dynamic runtime set value, ok if it becomes 'static' after initial use. (ie: nBreak shouldn't change once the for loop is started)
This was discussed at many Time.
The step is a fix value, it is set on compiletime, not at runtime. You can use a constant.
If Fred change this, the for:next is slower and can't optimized.
Why not use While : Wend or Repeat : Until.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
And, IIUC, that code is inefficient because it will re-check the length of the string which each iteration of the loop. You should put it in a variable first.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
ts-soft wrote:This was discussed at many Time.
The step is a fix value, it is set on compiletime, not at runtime. You can use a constant.
If Fred change this, the for:next is slower and can't optimized.
Why not use While : Wend or Repeat : Until.
If I remember correctly, Fred suggested something like this:
To use Len in a loop can quickly end in a disaster. Ok, if it's a short string, you will not check it. But for each doubling the len of the string, the running time will be about four times longer.
cString.s = Space (10000) ;test also with 20k and 40k
time = ElapsedMilliseconds()
For i = 1 To Len(cString)
x = 1
Next
MessageRequester ("", Str (ElapsedMilliseconds() - time))
Josh is right. You don't call Len() repeatedly if the size doesn't change. Calc it once, and use that value in the For/Next loop. In Josh's example above, and using Space(40000), the loop takes 700 ms on my PC. But if I calc Len() first and use that value in the loop, the code executes in 0 ms (instantly).
cString.s = Space (40000) ; 40k
time = ElapsedMilliseconds()
l=Len(cString)
For i = 1 To l
x = 1
Next
MessageRequester ("", Str (ElapsedMilliseconds() - time))
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!