For loop using variable step.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

For loop using variable step.

Post by jassing »

I was surprised I could not do this:

Code: Select all

For i = 1 To Len(cString) Step nBreak
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)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: For loop using variable step.

Post by ts-soft »

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.
Image
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: For loop using variable step.

Post by Tenaja »

There are some additional options here:
http://purebasic.fr/english/viewtopic.p ... 6&p=360605
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: For loop using variable step.

Post by Seymour Clufley »

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."
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: For loop using variable step.

Post by Nituvious »

What about something like this:

Code: Select all

myStep = 1
While (i<=15 - myStep): i + myStep
	
	If i = 5 : myStep = 5 : EndIf
	
	Debug "Loop count: " + Str(i)

Wend
[edit] oops, sorry. Looks like the linked thread already discusses this.
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
Lord
Addict
Addict
Posts: 899
Joined: Tue May 26, 2009 2:11 pm

Re: For loop using variable step.

Post by Lord »

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:

Code: Select all

A.f=1
E.f=10
myStep.f=0.5
For i=A To E Step 0
;Do whatever you want to do  
  Debug i
  i+myStep
Next
But I think, this is not the answer to the original question. :D
Image
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: For loop using variable step.

Post by Josh »

jassing wrote:

Code: Select all

For i = 1 To Len(cString)
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.

Code: Select all

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))
sorry for my bad english
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: For loop using variable step.

Post by MachineCode »

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).

Code: Select all

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!
Post Reply