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

    audi0615 - 2020-06-13


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

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

    FOR i:=0 to 999 DO
        array1[i]:=array2[indexnum]; //mapping

    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?



    Talk.ru: 1
    Talk.ru: 2

  • Comingback4u

    Comingback4u - 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

    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.


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



    Talk.ru: 1
    Talk.ru: 2

  • nothinrandom

    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.