Wish: Loop (x)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Wish: Loop (x)

Post by Trond »

Like:

Code: Select all

Loop 10
  AddElement(LinkedList())
EndLoop
Should add 10 (or 11?) elements to the linked list witohut having to use more variables.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

i see little win over

Code: Select all

for n = 1 to 10
  addelement(...)
next n
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Then you need another variable.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Code: Select all

Repeat
  AddElement(LinkedList())
Until CountList(LinkedList()) = 10
:)

Sorry (not really), just being hair-splitty.

I like/support the Loop n .. EndLoop idea. Maybe version 4.01:)
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Dare2 wrote:

Code: Select all

Repeat
  AddElement(LinkedList())
Until CountList(LinkedList()) = 10
:)

Sorry (not really), just being hair-splitty.

I like/support the Loop n .. EndLoop idea. Maybe version 4.01:)
I don't know how many elements which is currently in the list, but I found another way to do it without using an extra variable.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

But... for the program to keep track of how many times its looped, there's gonna be an internal variable anyway... so the only difference would be the way you type it. (ie zero benefit in the end)
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

If I understand right, yes, you need a caounter variable, like:

Code: Select all

Repeat
 AddElement(LinkedList())
 Counter+1
Until Counter=10
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

kenmo wrote:But... for the program to keep track of how many times its looped, there's gonna be an internal variable anyway... so the only difference would be the way you type it. (ie zero benefit in the end)
It would be easy to use a register for this, thus giving a speed boost. Also, if you don't care about the way you type it, you might as well program only in assembly.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I found another way to do it without using an extra variable

Which was? :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

      Repeat 
        AddElement(Memory())
      Until CountList(Neurons()) = CountList(Memory())
Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:D

In other words, 2.
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Two what?

And anyways it would still be faster speed-wise with this (optimized with the counter in a register):

Code: Select all

Loop CountList(Neurons()) - CountList(Memory())
  AddElement(Memory()) 
EndLoop
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

2 Lists. Or neurons :) Okay if you had the extra list to start with. Bit more than a variable in cost, otherwise.

I like the simplicity of the Loop n syntax.

However I can't see any real gain (eg, extra variable). Surely a register would need to be preserved (stacked or stored in memory) to be safe, there is no knowing what the code in between Loop and EndLoop does.

BTW, is there a point where this sort of thing self-defeating (not necessarily just this)? For eg, using 100 bytes of code to save 4 (or 8, or whatever) bytes of storage. Or of investing 6 months of messing around with code to improve execution speed by 1 clock per hour?
@}--`--,-- A rose by any other name ..
hardfalcon
User
User
Posts: 89
Joined: Fri Apr 29, 2005 3:03 pm
Location: Luxembourg
Contact:

Post by hardfalcon »

Code: Select all

Loop 10
  AddElement(LinkedList())
EndLoop
That could AFAIK be done with ASM (unfortunately I don't REALLY know how to code in ASM). But there was, if I remember right, an Op-Code which keeps on jumping to an offset which you can specify until a certain register reaches a certain value. But I'm just an ASM-N0000B..... :oops:
"And God caused a deep sleep to fall upon Adam, and he slept: and he took one of his ribs, and closed up the flesh instead thereof; And the spare rib, which God had taken from man, made he a woman, and brought her unto the man"
Post Reply