How to Optimize the Array Operations

Started by
3 comments, last by Dave1024 14 years, 11 months ago
Any coding technique possible for optimize the following array operation by avoiding the intermediate saving of values Array Operation:: pr = abc[0] + abc[2]; qr = abc[0] - abc[2]; pi = abc[1] + abc[3]; qi = abc[1] - abc[3]; rr = abc[4] + abc[6]; sr = abc[4] - abc[6]; ri = abc[5] + abc[7]; si = abc[5] - abc[7]; abc[0] = pr + rr; abc[4] = pr - rr; abc[1] = pi + ri; abc[5] = pi - ri; abc[2] = qr + si; abc[6] = qr - si; abc[3] = qi - sr; abc[7] = qi + sr;
Advertisement
Sure, one can reduce the number of intermediate variables involed in such a thing. Lets see if I can even get my futile attempt correct.
abc4   = abc[4];abc4m6 = abc[4] - abc[6];abc0m2 = abc[0] - abc[2];abc5   = abc[5];abc1m3 = abc[1] - abc[3];abc[4] = abc[0] + abc[2] - abc[4] - abc[6];abc[0] = abc[0] + abc[2] + abc4   + abc[6];abc[5] = abc[1] + abc[3] - abc[5] - abc[7];abc[1] = abc[1] + abc[3] + abc5   + abc[7];abc[2] = abc0m2 + abc5 - abc[7];abc[6] = abc0m2 - abc5 + abc[7];abc[3] = abc1m3 - abc4m6;abc[7] = abc1m3 + abc4m6;

Ugly huh! I don't think it's worth it. Speed is certainly not measured by the number of variables in your code. It's probably both cleaner and more efficient how you have it already. Is it not the compiler's job to optimise basic things like this for you?

What suggests there is any value in even attempting such a change, and what does your profiling tell you about the speed of this compared to your original code?

Other things might be possible if we could see the real code. We don't even know what the data type involved here is. What does the function this is in do and what does it really look like?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
No. Except in extremely unusual circumstances (which are not the case here), the result of operation like a+b needs to be stored by the processor before it can be used, and there's no way around that.

If you have access to SIMD, you can perform the sums and subtractions together.
Yes, there are complex ways of doing array operations that are more efficient on large matrices.

For small matrices, the overhead of these tricks tends to out weigh the better O-notation performance of the more complex ways of multiplying matrices and the like.

The intermediate saving of values isn't what makes the operation slow. Instead, you cleverly find intermediate values that can be used in multiple contexts and pre calculate them.

For an example in a different context, you can speed up polynomial multiplication:
(a X^n + b) * (c X^n + d)
= (ac) X^2n + (ad + bc) X^n + bd
That naive solution is O(n^2) in the size of the polynomials being multiplied.

Now, K = (a+b)*(c+d) = ac + bc + ad + bd
so we get:
= (ac) X^2n + (K - ac - bd) X^n + bd
as our answer, and we have reduced the number of n-sized multiplications from 4 to 3 (in exchange for going from 1 addition to 4 additions) to multiply two 2n-order polynomials.

This improves performance to O(n^q), where q is a number between 1 and 2 that I don't recall.

For actual faster matrix multiplication algorithms,
http://en.wikipedia.org/wiki/Matrix_multiplication
under "Algorithms for efficient matrix multiplication" has some. Note the open research topic linked there. :)


Thanks to ur replays!
I have some more points to add

a)The data type used for array 'abc' is int

b)while in a 'Single Instruction Single Data' Processor how this array operations to be optimized if this array operations can iterate in a loop of size 20

c)Speeding up the polynomiyal operation is effective in the above cases?

This topic is closed to new replies.

Advertisement