IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7870


Ignore:
Timestamp:
Jul 11, 2006, 5:46:11 PM (20 years ago)
Author:
Paul Price
Message:

Optimising code by removing unnecessary allocations, and dereferencing pointers for loops.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psStats.c

    r7766 r7870  
    1616 * use ->min and ->max (PS_STAT_USE_RANGE)
    1717 *
    18  *  @version $Revision: 1.176 $ $Name: not supported by cvs2svn $
    19  *  @date $Date: 2006-06-30 02:20:06 $
     18 *  @version $Revision: 1.177 $ $Name: not supported by cvs2svn $
     19 *  @date $Date: 2006-07-12 03:46:11 $
    2020 *
    2121 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    172172    long count = 0;                     // Number of points contributing to this mean
    173173
     174    psF32 *data = myVector->data.F32;   // Dereference
     175    int numData = myVector->n;          // Number of data points
     176
    174177    // If PS_STAT_USE_RANGE is requested, then we enter a slightly different loop.
    175178    if (!errors) {
     
    177180            if (maskVector) {
    178181                // No errors, check range, check mask
    179                 for (long i = 0; i < myVector->n; i++) {
     182                psU8 *maskData = maskVector->data.U8;
     183                for (long i = 0; i < numData; i++) {
    180184                    // Check if the data is with the specified range
    181                     if (!(maskVal & maskVector->data.U8[i]) &&
    182                             (stats->min <= myVector->data.F32[i]) &&
    183                             (myVector->data.F32[i] <= stats->max)) {
    184                         mean += myVector->data.F32[i];
     185                    if (!(maskVal & maskData[i]) && stats->min <= data[i] && data[i] <= stats->max) {
     186                        mean += data[i];
    185187                        count++;
    186188                    }
    187189                }
    188 
    189190            } else {
    190191                // No errors, check range, no mask
    191                 for (long i = 0; i < myVector->n; i++) {
    192                     if ((stats->min <= myVector->data.F32[i]) &&
    193                             (myVector->data.F32[i] <= stats->max)) {
    194                         mean += myVector->data.F32[i];
     192                for (long i = 0; i < numData; i++) {
     193                    if (stats->min <= data[i] && data[i] <= stats->max) {
     194                        mean += data[i];
     195                    }
     196                }
     197                count = numData;
     198            }
     199        } else {
     200            if (maskVector) {
     201                // No errors, no range check, check mask
     202                psU8 *maskData = maskVector->data.U8;
     203                for (long i = 0; i < numData; i++) {
     204                    if (!(maskVal & maskData[i])) {
     205                        mean += data[i];
    195206                        count++;
    196207                    }
    197208                }
    198             }
    199         } else {
    200             if (maskVector) {
    201                 // No errors, no range check, check mask
    202                 for (long i = 0; i < myVector->n; i++) {
    203                     if (!(maskVal & maskVector->data.U8[i])) {
    204                         mean += myVector->data.F32[i];
    205                         count++;
    206                     }
    207                 }
    208209
    209210            } else {
    210211                // No errors, no range check, no mask
    211                 for (long i = 0; i < myVector->n; i++) {
    212                     mean += myVector->data.F32[i];
    213                 }
    214                 count = myVector->n;
     212                for (long i = 0; i < numData; i++) {
     213                    mean += data[i];
     214                }
     215                count = numData;
    215216            }
    216217        }
     
    224225    } else {
    225226        psF32 sumWeights = 0.0;         // The sum of the weights
     227        psF32 *errorsData = errors->data.F32;
    226228        if (stats->options & PS_STAT_USE_RANGE) {
    227             if (maskVector != NULL) {
    228                 for (long i = 0; i < myVector->n; i++) {
     229            if (maskVector) {
     230                psU8 *maskData = maskVector->data.U8;
     231                for (long i = 0; i < numData; i++) {
    229232                    // Check if the data is with the specified range
    230                     if (!(maskVal & maskVector->data.U8[i]) &&
    231                             (stats->min <= myVector->data.F32[i]) &&
    232                             (myVector->data.F32[i] <= stats->max)) {
    233                         float weight = 1.0 / PS_SQR(errors->data.F32[i]);
    234                         mean += myVector->data.F32[i] * weight;
     233                    if (!(maskVal & maskData[i]) && stats->min <= data[i] && data[i] <= stats->max) {
     234                        float weight = 1.0 / PS_SQR(errorsData[i]);
     235                        mean += data[i] * weight;
    235236                        sumWeights += weight;
    236237                        count++;
     
    239240            } else {
    240241                for (long i = 0; i < myVector->n; i++) {
    241                     if ((stats->min <= myVector->data.F32[i]) &&
    242                             (myVector->data.F32[i] <= stats->max)) {
    243                         float weight = 1.0 / PS_SQR(errors->data.F32[i]);
    244                         mean += myVector->data.F32[i] * weight;
     242                    if (stats->min <= data[i] && data[i] <= stats->max) {
     243                        float weight = 1.0 / PS_SQR(errorsData[i]);
     244                        mean += data[i] * weight;
    245245                        sumWeights += weight;
    246246                        count++;
     
    249249            }
    250250        } else {
    251             if (maskVector != NULL) {
     251            if (maskVector) {
     252                psU8 *maskData = maskVector->data.U8;
    252253                for (long i = 0; i < myVector->n; i++) {
    253                     if (!(maskVal & maskVector->data.U8[i])) {
    254                         float weight = 1.0 / PS_SQR(errors->data.F32[i]);
    255                         mean += myVector->data.F32[i] * weight;
     254                    if (!(maskVal & maskData[i])) {
     255                        float weight = 1.0 / PS_SQR(errorsData[i]);
     256                        mean += data[i] * weight;
    256257                        sumWeights += weight;
    257258                        count++;
     
    260261            } else {
    261262                for (long i = 0; i < myVector->n; i++) {
    262                     float weight = 1.0 / PS_SQR(errors->data.F32[i]);
    263                     mean += myVector->data.F32[i] * weight;
     263                    float weight = 1.0 / PS_SQR(errorsData[i]);
     264                    mean += data[i] * weight;
    264265                    sumWeights += weight;
    265266                }
     
    731732
    732733    if (errors) {
    733         #if 1
    734         // XXX: The ADD specifies this as the definition of the standard
    735         // deviation if the errors are known.  Verify this with IfA: none of
    736         // the data points in the vector are used.  Verify that the masks and
    737         // data ranges are used correctly.
    738734        stats->sampleStdev = (1.0 / sqrtf(errorDivisor));
    739         #else
    740 
    741         stats->sampleStdev = sqrt((sumSquares - (sumDiffs * sumDiffs / (float)count)) /
    742                                   (float)(count - 1));
    743         #endif
    744 
    745735    } else {
    746736        stats->sampleStdev = sqrt((sumSquares - (sumDiffs * sumDiffs / (float)count)) /
    747737                                  (float)(count - 1));
    748 
    749738    }
    750739    psTrace(__func__, 4, "---- %s() end ----\n", __func__);
     
    789778    psStats *statsTmp = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
    790779
    791     // We've already copied the mask vector, so it's safe to simply increment the reference counter.
    792     psVector *tmpMask = NULL;
     780    // We copy the mask vector, to preserve the original
     781    psVector *tmpMask = psVectorAlloc(myVector->n, PS_TYPE_U8);
     782    tmpMask->n = tmpMask->nalloc;
     783    psVectorInit(tmpMask, 0);
    793784    if (maskVector) {
    794         tmpMask = psMemIncrRefCounter(maskVector);
    795     } else {
    796         tmpMask = psVectorAlloc(myVector->n, PS_TYPE_U8);
    797         tmpMask->n = tmpMask->nalloc;
    798         psVectorInit(tmpMask, 0);
     785        for (long i = 0; i < myVector->n; i++) {
     786            if (maskVector->data.U8[i] & maskVal) {
     787                tmpMask->data.U8[i] = 0xff;
     788            }
     789        }
    799790    }
    800791
     
    840831        if (errors) {
    841832            for (long j = 0; j < myVector->n; j++) {
    842                 if (! tmpMask->data.U8[j] & maskVal &&
     833                if (!tmpMask->data.U8[j] &&
    843834                        fabsf(myVector->data.F32[j] - clippedMean) > stats->clipSigma * errors->data.F32[j]) {
    844835                    tmpMask->data.U8[j] = 0xff;
     
    851842        } else {
    852843            for (long j = 0; j < myVector->n; j++) {
    853                 if (! tmpMask->data.U8[j] & maskVal &&
     844                if (!tmpMask->data.U8[j] &&
    854845                        fabsf(myVector->data.F32[j] - clippedMean) > (stats->clipSigma * clippedStdev)) {
    855846                    tmpMask->data.U8[j] = 0xff;
     
    11311122    tmpScalar.type.type = PS_TYPE_F32;
    11321123
    1133     // We need a mask for these statistics, so if the one we're given is NULL, make our own.
    1134     psVector *mask = NULL;              // The actual mask we will use
     1124    psVector *mask = psVectorAlloc(myVector->n, PS_TYPE_MASK); // The actual mask we will use
     1125    mask->n = myVector->n;
     1126    psVectorInit(mask, 0);
    11351127    if (maskVector) {
    1136         mask = psMemIncrRefCounter(maskVector);
    1137     } else {
    1138         mask = psVectorAlloc(myVector->n, PS_TYPE_U8);
    1139         mask->n = myVector->n;
    1140         psVectorInit(mask, 0);
     1128        for (long i = 0; i < myVector->n; i++) {
     1129            if (maskVector->data.U8[i] & maskVal) {
     1130                mask->data.U8[i] = 0xff;
     1131            }
     1132        }
    11411133    }
    11421134
     
    14401432    long N50 = 0;
    14411433    for (long i = 0 ; i < myVector->n ; i++) {
    1442         if (!(mask->data.U8[i] & maskVal) &&
     1434        if (!mask->data.U8[i] &&
    14431435                (binLo25F32 <= myVector->data.F32[i]) && (binHi25F32 >= myVector->data.F32[i])) {
    14441436            N50++;
     
    20472039    psVector *maskU8 = NULL;            // Input mask vector, U8 version
    20482040    if (mask) {
    2049         // We want a copy of the mask, since we may change values
    2050         maskU8 = psVectorCopy(NULL, mask, PS_TYPE_U8);
    2051     } else {
    2052         maskU8 = psVectorAlloc(in->n, PS_TYPE_U8);
    2053         maskU8->n = in->n;
    2054         memset(maskU8->data.U8, 0, in->n * sizeof(psU8));
    2055     }
    2056 
    2057     // Mask bad data
    2058     for (long i = 0; i < inF32->n; i++) {
    2059         if (!isfinite(inF32->data.F32[i])) {
    2060             maskU8->data.U8[i] = 0xff;
     2041        if (mask->type.type == PS_TYPE_MASK) {
     2042            maskU8 = psMemIncrRefCounter((psPtr)mask);
     2043        } else {
     2044            maskU8 = psVectorCopy(NULL, mask, PS_TYPE_MASK);
    20612045        }
    20622046    }
Note: See TracChangeset for help on using the changeset viewer.