# 在C中沿第n维求最大值

I'm working on a problem where the execution time is critical. I have another C function that produces 3-D grids of values at a series of timestamps. What I want is to find the `max_value` in each 3-D grid at each timestamp. Additionally I am tracking the average value (`sum / ncell`) of each grid, and returning a maximum normalised by the average value.

Currently, I am timing my Python wrapper function (which includes the initialisation of the `numpy.ndarray`s that receive the max, normMax, and maxIndex values:

• m = 2400
• n = 54000
• 线程= 8

• 英特尔®酷睿TM i7-7700HQ CPU @ 2.80GHz（6MB缓存）
• 32GB RAM

``````void find_max(double *mapPt, double *maxPt, double *normMaxPt,
int64_t *indexPt, int32_t nsamp, int32_t ncell,
{
double  maxValue, currentValue, sum;
int32_t cell, maxIndex, timeSample;

for (timeSample=0; timeSample<nsamp; timeSample++)
{
maxValue = 0.0;
maxIndex = 0;
sum = 0.0;
for (cell=0; cell<ncell; cell++)
{
currentValue = mapPt[cell * nsamp + timeSample];
sum += currentValue;
if (currentValue > maxValue)
{
maxValue = currentValue;
maxIndex = cell;
}
}
maxPt[timeSample] = maxValue;
normMaxPt[timeSample] = maxValue * ncell / sum;
indexPt[timeSample] = maxIndex;
}
}
``````

One suggestion I could see would be to have `double *timesame_mapcells = &mapPt[timeSample];` at the start of every thread.

Then you can just index with `cell * nsamp`, so one addition less per access. But the compiler might have been clever enough to optimize that.

``````for (cell = 0, map_idx = timeSample; cell < ncell; cell++, map_idx += nsamps)
{
currentValue = mapPt[map_idx];
[...]
}
``````