Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

data mapping array without loop

audi0615
2020-06-13
2020-07-25
  • audi0615 - 2020-06-13

    Hi.

    Suppose i have 2 arrays,

    array1 : array [0..999] of real;
    array2 : array [0..999] of real;
    

    Components of array2 need to be mapped into array1.
    For this, in text file i have made mapping data as below.

    0=50 (means array1[0]:=array2[50])
    1=90
    2=100
    ...

    by reading the text file, i have stored index numbers of array2 to another array.

    arIndex[0]:=50;
    arIndex[1]:=900;
    arIndex[2]:=100;
    ......
    
    FOR i:=0 to 999 DO
    
        indexnum:=arIndex[i];
        array1[i]:=array2[indexnum]; //mapping
    
    END_FOR
    

    My question is, is it possible to do this kind of mapping without using loop? (by using pointer or reference??)
    the problem is the number of data could be 100,000 or more and for loop might cause a problem such as delay, CPU usage...
    Maybe there are some "memory level" ways of mapping which is very fast. Do you have any idea?

     

    Related

    Talk.ru: 1
    Talk.ru: 2

  • Lo5tNet - 2020-06-15

    Its been a while since I've tested memcpy against a loop but if memory serves me correctly using memcpy wasn't any faster which lead me to believe it was just doing the loop for me. Anyone know if this is true?

     
  • wollvieh

    wollvieh - 2020-06-15

    Just checked it with Twincat3, memcpy is 3 times faster then a loop. I checked this by copying 1000000 real from one array to another.

     

    Last edit: wollvieh 2020-06-15
  • audi0615 - 2020-06-15

    Thanks Gentlemen.

    I don't understand why loop is not required if memcpy is used.
    memcpy just copies one block of memory to another which can be used to copy a whole array to another one.
    however in my case, it has to map values of array components to another as below.

    array1[0]:=array2[50]
    array1[1]:=array2[90]
    array1[2]:=array2[100]
    ....
    

    Could you please show me an example of code using memcpy for data mapping?

     

    Related

    Talk.ru: 1
    Talk.ru: 2

  • nothinrandom - 2020-07-25

    @audi0615, from library SysMem, SysMemCpy is relatively straight forward. You have three inputs: pDest (destination array), pSrc (source array), and udiCount (total bytes). You will need to use ADR to get reference pointer.

    SysMemCpy(pDest:=ADR(array2), pSrc:=ADR(array1), udiCount:=4000); // 4*1000

     

Log in to post a comment.