FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Improve Code
Posts: 153
Joined: Tue Aug 05, 2014 09:48 AM
Improve Code
Posted: Thu Dec 26, 2019 05:09 AM
Hi
How can I improve code using AEval instead of For Next loop
Code (fw): Select all Collapse
#include "fivewin.ch"

function Main()

   local aData := {}
   local i, j,  aTemps2 := {}

FOR i := 1 TO  50
    FOR j := 1 TO  26
      aAdd(aTemps2, hb_valtostr("Demo "+alltrim(str(i))+" "+alltrim(str(j))))
    NEXT j
    aAdd( aData,  aTemps2)
    aTemps2 := {}
NEXT i

xbrowser adata

return nil
Regards, Greetings



Try FWH. You will enjoy it's simplicity and power.!
Posts: 375
Joined: Tue Feb 10, 2015 09:48 AM
Re: Improve Code
Posted: Thu Dec 26, 2019 07:43 AM
Nice quiz, for me this solution should works,
Code (fw): Select all Collapse
local aData := Array(50,26)
aEval(aData, {|x,i| aEval(aData[i], {|y,j| aData[i,j]:=hb_valtostr("Demo "+alltrim(str(i))+" "+alltrim(str(j))) })} )

but it spawns an error:
Code (fw): Select all Collapse
Error E0005  Outer codeblock variable 'I' is out of reach

that I don't undestand.
but I can fix it, in this way:
Code (fw): Select all Collapse
local aData := Array(50,26), i
aEval(aData, {|x,ii| i:=ii, aEval(aData[i], {|y,j| aData[i,j]:=hb_valtostr("Demo "+alltrim(str(i))+" "+alltrim(str(j))) })} )
Posts: 153
Joined: Tue Aug 05, 2014 09:48 AM
Re: Improve Code
Posted: Thu Dec 26, 2019 08:53 AM

Thank you

Regards, Greetings



Try FWH. You will enjoy it's simplicity and power.!
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Improve Code
Posted: Thu Dec 26, 2019 09:23 AM
This is the most efficient code, I think:

Code (fw): Select all Collapse
#include "fivewin.ch"

function Main()

    local aData[ 50000, 26 ]
    local nSec
    local i, j

    nSec = SECONDS()

    FOR i := 1 TO  50000
        FOR j := 1 TO  26
            aData[ i, j ] = "Demo "+ltrim(str(i))+" "+ltrim(str(j))
        NEXT j
    NEXT i

    ? SECONDS() - nSec

    xbrowser adata

    return nil


EMG
Posts: 375
Joined: Tue Feb 10, 2015 09:48 AM
Re: Improve Code
Posted: Fri Dec 27, 2019 06:20 PM
Sure, it is better pre-allocate the array of the right size then fill it instead of do aAdd more and more.
If you don't know the final size you can pre-allocate of a big size then use aSize of right dimension, like
Code (fw): Select all Collapse
aData:=Array(1000)
j:=1
for i:=1 to 1000
   if( something )
     aData[j++]:= value
   endif
next
aSize(@aData,j)


For a correct measure you should include the array creation.
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Improve Code
Posted: Fri Dec 27, 2019 06:37 PM
AntoninoP wrote:For a correct measure you should include the array creation.


Right. I tried: no change.

EMG

Continue the discussion