I'm not sure why you can't use the optimiser since, in my experience, it will usually produce faster code than the vast majority of "wanna-be" manual optimisers :-) In addition, you should make sure that this code is actually a problem area - there's no point optimising code that's already close to maximum speed, nor should you concern yourself with something that accounts for 0.01% of the time taken when there may be code elsewhere responsible for 20%.
Optimisation should be heavily targeted otherwise it's wasted effort.
Any solution other than the naive "just add the numbers together" will most likely have to use special features in the target CPU.
Provided you're willing to take a small hit on each update to the array (and this may not be an option given your "all values have been initialized" comment), you can get the sum in very quick time. Use a "class" to maintain the array and the sum side-by-side. Pseudo-code like:
def initArray (sz):
allocate data as sz+1 integers
foreach i 0 thru sz:
set data[i] to 0
def killArray(data):
free data
def getArray (data,indx):
return data[indx+1]
def setArray (data,indx,val):
data[0] = data[0] - data[indx] + val
data[indx+1] = val
def sumArray(data):
return data[0]
should do the trick.
The following complete C program shows a very rough first-cut which you can use as a basis for a more robust solution:
#include <stdio.h>
#include <stdlib.h>
static int *initArray (int sz) {
int i;
int *ret = malloc (sizeof (int) * (sz + 1));
for (i = 0; i <= sz; i++)
ret[i] = 0;
return ret;
}
static void killArray(int *data) {
free (data);
}
static int getArray (int *data, int indx) {
return data[indx+1];
}
static void setArray (int *data, int indx, int val) {
data[0] = data[0] - data[indx] + val;
data[indx+1] = val;
}
static int sumArray (int *data) {
return data[0];
}
int main (void) {
int i;
int *mydata = initArray (10);
if (mydata != NULL) {
setArray (mydata, 5, 27);
setArray (mydata, 9, -7);
setArray (mydata, 7, 42);
for (i = 0; i < 10; i++)
printf ("Element %d is %3d\n", i, getArray (mydata, i));
printf ("Sum is %3d\n", sumArray (mydata));
}
killArray (mydata);
return 0;
}
The output of this is:
Element 0 is 0
Element 1 is 0
Element 2 is 0
Element 3 is 0
Element 4 is 0
Element 5 is 27
Element 6 is 0
Element 7 is 42
Element 8 is 0
Element 9 is -7
Sum is 62
As I said, this may not be an option but, if you can swing it, you'll be hard-pressed finding a faster way to get the sum than a single array index extraction.
And, as long as you're implementing a class to do this, you may as well use the first two elements for housekeeping, one for the current sum and one for the maximum index, so that you can avoid out-of-bounds errors by checking indx against the maximum.
fork?-msse4