开发者

Improve processing time for array calculation

The code look like this sum += array开发者_如何学C[j] + array[j+1] + array [j + 2]+ ... array[j + n]; how do I replace the j+n inside the bracket to improve the timing?


You don't do this. Unless you have a brain-dead compiler, it should be able to optimise this quite adequately.

If you're going to do this level of micro-optimisation, you need to start looking at the underlying assembly code, not assuming that your compiler will blindly translate your code into exactly-equivalent assembly code.

You will also need to understand the intricacies of your target platform better than the people that write your compiler which is frankly, based on the insane code I've seen coming from gcc in high optimisation levels, unlikely :-)

You'll usually get a better return on investment if you concentrate on big-picture optimisations like algorithm selection and so forth.

What you should do (if you haven't already) is profile the code, once finished, to see where the bottlenecks are (and only if it's underperforming: there's no point in optimising something that's already running fast enough).

Then concentrate on those bottlenecks. Measure, don't guess!


By way of example, I was actually going to show you how well optimised the following code became:

#include <stdio.h>
int main(void) {
    int j, sum, array[50];
    for (j = 0; j < 50; j++)
        array[j] = 999 - j * 2;
    j = 22;
    sum = array[j] + array[j+1] + array [j + 2] + array[j + 3];
    return 0;
}

but under gcc optimisation level 3, it became:

main:
    pushl   %ebp         ; prolog
    xorl    %eax, %eax   ; return value
    movl    %esp, %ebp   ; epilog 1
    popl    %ebp         ; epilog 2
    ret

Yes, that's right, it's just the stack prolog and epilog code and setting of the return value, with no calculations in sight. gcc has (rightly) figured out that none of the calculations are used anywhere so has optimised them totally out of existence.


Once you use it, the relevant code becomes a simple:

movl    116(%esp), %eax
addl    112(%esp), %eax
addl    120(%esp), %eax
addl    124(%esp), %eax

and you'd be hard-pressed getting it much more optimised than that.


Why not

T* aj = &(array[j]);
sum = aj[0] + aj[1] + ...

?


int *aj

aj = &(array[j]);

is correct, and the sum is equal to

sum += *aj + *(aj+1) + *(aj+2) ....

It defenitly would perform your code if you start whit

aj = &(array[0]);

and then just make the adds to aj

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜