Decreasing FOR loops..

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Decreasing FOR loops..

Post by dagcrack »

Apart from wanting more FOR functionality (like in other languages) I also would also like to see a descrease option... because decreasing FOR loops are faster than increasing ones. This is a MUST have.

So, something like:

For l=1000 to 0
Next

Should be faster than an increasing value, as on every iteration "the loop" should check wether L equals 0 and if it does, then break the loop... This is faster than comparing the value L with the actual iteration number, isnt it?, should be in ASM..

For when, Fred? :)
(Or is it possible by now?, Don't think so though).
After all, we like the high-level functions of PB.. but we want the functionality of lower-level languages too, be it there for the ones who needs it.

Don't make us do it by ourselves :lol:
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Re: Decreasing FOR loops..

Post by va!n »

!? its possible...

Code: Select all

For l=1000 to 0 step -1
Next 
is this what you mena!? i know the issue you are talking about... i have read some articles about this too... but i dont know how purebasic will convert its to asm...


Edited:
corrected 1 to -1
Last edited by va!n on Wed Oct 19, 2005 12:53 pm, edited 1 time in total.
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

I doubt you'd get much from using it in a for loop, because you'd still have to maintain the loop variable for use in the program.

Perhaps it would good to be able to have something like

Code: Select all

Do 1000

Loop
With the loop being highly optimised.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

And btw ...

@va!n:

shouldn't it be Step -1:

Code: Select all

For l=1000 to 0 step -1
Next
regards,
benny!
-
pe0ple ar3 str4nge!!!
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

benny wrote: @va!n:
shouldn't it be Step -1:
@benny:
sure! ;) corrected, small typemistake...

@GedB:
Sorry, but i dont see there any different... using Repeat/Until Do/Loop will do internaly the same, isnt it? And i would not like to see another way for Repeat/Until... We still have it and i like the way how to use it and the commands name!
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@va!n:

Maybe Geb_B speaks about the asm-command LOOP whichs decrements
ECX and branches to a label (loops) as long as ECX is not zero.

So something like this :

Code: Select all

i.l = 10
x.l = 0
!XOR  ecx, ecx
!MOV  ecx, dword[v_i]
!_LOOPSTART:
  x+1
!LOOP _LOOPSTART
!MOV  dword[v_i], ecx
Debug x

Generally speaking I do not know if the above makes sense or is faster
than the one produced by PureBasic. I think such optimazations should
be done in external tools, like the one by remi and/or Deem2031.

IMHO the official development of Purebasic should concentrate on the de-
velopment of new functionality / features.

Just my 2 cents ;-)
regards,
benny!
-
pe0ple ar3 str4nge!!!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

I = 1000
While I
  !DEC[v_I]
  Debug I
Wend
another fast way, i hope
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
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

benny wrote: Maybe Geb_B speaks about the asm-command LOOP whichs decrements ECX and branches to a label (loops) as long as ECX is not zero.
@benny:
Ohhhh, ok... its possible that he means the ASM Loop command but it doesnt looked so at the first moment for me ;)
benny wrote:...I think such optimazations should be done in external tools, like the one by remi and/or Deem2031. IMHO the official development of Purebasic should concentrate on the development of new functionality / features.
@benny:
sorry i have to disagree with you! i think when such optimisations (in general) are possible, why shouldnt they implented directly inside purebasic!? Its nice to have such tools as from remi_meier and Deeem2031 but it would be much greater and cleaner/professional when PureBasic do this job and not any 3rd part tool "hacking/modify" your source without any guarantee...

I think before adding any new 2d/3d or any other extended stuff to purebasic, its very important to solve known bugs, like strings in threads, and that the mainengine of purebasic supports more different types lile doubles, unsigned/signed.... in my opinion this is the most importants basic for any sdk - if this is done, all other stuff can be added! ;)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@va!n:

Ok. Having other priorities in mind - I totally agree with you to complete and
remove any bugs from the purebasic core. First a solid core should exits
before spending too much time on other complex matters like 3D for example.

Nevertheless, considering the fact that Purebasic exists for multiple OS and
it is a commercial application that competes against other languages like
BlitzBasic, I think Fred found a good way so far and built a damn cool system/
language.


However, back to topic ;-)


I did a quick test on that LOOP thing and it seems to be pretty lame compared
to the normal FOR-construct created by Purebasic. So forget the source
I wrote above :roll:

ts-soft's version seems to be pretty much the same as the normal FOR-
construct.
The generated ASM Code for a FOR/NEXT Loop looks like this:

Code: Select all

  i.l = 10
!_For1:
  !XOR    eax,eax
  !CMP    eax,dword [v_i]
  !JG    _Next2
; Next i
!_NextContinue2:
  !DEC    dword [v_i]
  !JMP   _For1
!_Next2:
whereas the generated WHILE-Loop is something like this:

Code: Select all

 i.l = 10
; While (i)
!_While1:
  !CMP    dword [v_i],0
  !JE    _Wend1
;!DEC[v_i]
; i-1
  !DEC    dword [v_i]
; Debug i
; Wend
  !JMP   _While1
!_Wend1:
So, acutally the same just the XOR-command is missing and the comparision
is between value and a constant which could make it slightly faster :!: But
maybe an assembler pro wants to comment that.

And btw, @ts-soft:

There isn't any different between !DEC[v_i] and i-1, is it :?:
regards,
benny!
-
pe0ple ar3 str4nge!!!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

i hoped my code was fast :?
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
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

ts-soft wrote:i hoped my code was fast :?
It is ... in my eyes :!:
regards,
benny!
-
pe0ple ar3 str4nge!!!
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

Could this be counted as a test :roll:

Debugger 0ff

Code: Select all

Delay(1000)

  t1.l = ElapsedMilliseconds()
  i.l =  999999999
  x.l = 0
  !XOR  ecx, ecx
  !MOV  ecx, dword[v_i]
  !_LOOPSTART:
    ;x+1
  !INC  dword[v_i]
  !LOOP _LOOPSTART
  !MOV  dword[v_i], ecx
  t2 = ElapsedMilliseconds() - t1

Delay(1000)

  t3.l = ElapsedMilliseconds()
  i.l = 999999999
  !_For1:
    !XOR    eax,eax
    !CMP    eax,dword [v_i]
    !JG    _Next2
  ; Next i
  !_NextContinue2:
    !DEC    dword [v_i]
    !JMP   _For1
  !_Next2:
  t4 = ElapsedMilliseconds() - t3
  
Delay(1000)

  t5.l = ElapsedMilliseconds()
   i.l = 999999999
  ; While (i)
  !_While1:
    !CMP    dword [v_i],0
    !JE    _Wend1
  ;!DEC[v_i]
  ; i-1
    !DEC    dword [v_i]
  ; Debug i
  ; Wend
    !JMP   _While1
  !_Wend1:
  t6 = ElapsedMilliseconds() - t5


  MessageRequester("Speed", "LOOP-Methode: "+Str(t2)+Chr(13)+Chr(10)+"FOR-Methode: "+Str(t4)+Chr(13)+Chr(10)+"WHILE-Methode"+Str(t6))
regards,
benny!
-
pe0ple ar3 str4nge!!!
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Well...

You get the same thing (though with more operations) by doing:

Code: Select all


For i = 0 to 1000
	ii = 1000-i
	;do whatever
next

va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@benny:
ok, no problem ;) Sure, PureBasic is a nice language but there is a lot of work to do! :wink:

Here are my results of your speed comparsion test:

Code: Select all

LOOP-Methode: 4093
FOR-Methode: 4391
WHILE-Methode: 4406
Sadly but i cant use /COMMENTED - does not works here for any reason ;(


[Edited:]
Maybe it make more sence to compare LOOP/FOR/WHILE with increasind and decreasing values...!? ;)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@va!n:

Strange :!: Here (AMD Duron 1.3Mhz) it is the complete opposite. The Loop
Version is the slowest whereas the FOR/NEXT is nearly as fast as the WHILE
method.

Maybe it is hardware (chip) specific ... but maybe I should stop talking now
because I didn't touched any assembly language since I left the Amiga scene :roll:

Maybe some of our optimization-specialists can comment anything to
this :?:
regards,
benny!
-
pe0ple ar3 str4nge!!!
Post Reply