Thread: Taking forever to copy 2d array

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    No.What i am saying is that you have a code where you first copy, then do work and then copy again.
    This is translated to a double for loop , a tri- for loop and another double for loop.The tri - for loop will be executed 4*4*4 = 64 times ,where the double for loop will be executed 4*4 = 16 times. So which one is that affects your performance more?The one that is executed more times than the other, thus the tri - for loop.

    But myself i do not believe that these operations were about to take too long.

    What about the functions mixcolumnslookup1 and mixcolumnslookup2 ? Maybe one of them is what makes your code too slow.

    Code:
    if (!has0or1)
    {
             int added = mixcolumnslookup1(charstate[xorresult][mxcol])+mixcolumnslookup1(multmatrix[mxrow][xorresult]);
             if (added > 0xFF)
                    added = added- 0xFF;
             unsigned  char final = added;
             mxthenxor[xorresult] = mixcolumnslookup2(final);
    }

  2. #17
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Both pieces of code are identical except for where charstate is copied from. The lookup functions are executed in both.

  3. #18
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Ah the answer seems to be that I was optimising for speed and when the data is staying the same it makes it faster.

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by andydufresne View Post
    Both pieces of code are identical except for where charstate is copied from. The lookup functions are executed in both.
    I wouldn't count on it. Just because the code is still there doesn't mean it isn't optimised out. When you stop using the result of code that otherwise has no side effects, then the compiler will sometimes sneakily remove all the code that it sees has no effect. Optimisers are good like that.
    That's probably why you were mislead into thinking it was the copy that took a long time. In reality the time taken to copy 16 bytes wil be much less that the work that is done 64-times, which includes function calls and lots of probably unpredicatable branching.

    I wouldn't begin to optimse this without seeing what happens in those function calls.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Quote Originally Posted by iMalc View Post
    I wouldn't count on it. Just because the code is still there doesn't mean it isn't optimised out. When you stop using the result of code that otherwise has no side effects, then the compiler will sometimes sneakily remove all the code that it sees has no effect. Optimisers are good like that.
    That's probably why you were mislead into thinking it was the copy that took a long time. In reality the time taken to copy 16 bytes wil be much less that the work that is done 64-times, which includes function calls and lots of probably unpredicatable branching.

    I wouldn't begin to optimse this without seeing what happens in those function calls.
    Ah, thank you! I though I'd found some way of making my program much more efficient! BTW are there any suggestions you'd make about improving the performance of the substantive code?

  6. #21
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by andydufresne View Post
    Ah, thank you! I though I'd found some way of making my program much more efficient! BTW are there any suggestions you'd make about improving the performance of the substantive code?
    How about those on post number 4?
    You gave it a try and did not see difference?You didn't try them at all?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-04-2012, 07:58 PM
  2. Input to array loops forever [C]
    By Quixiotic in forum C Programming
    Replies: 5
    Last Post: 11-08-2011, 12:59 PM
  3. Taking a variable and input each value of it into an array
    By jammysammy in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 04:46 AM
  4. Replies: 1
    Last Post: 04-08-2011, 11:22 AM
  5. Int Array - Stop taking values at [zero]
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-04-2008, 12:54 AM