加快这种标准偏差方法

时间:2021-09-29 21:24:07

I have this method to calculate the standard deviation of an array of NSNumber integers, given a mean. The calculation uses NSDecimals to retain the highest resolution. This is currently demanding many cpu cycles, any help to speed it up while retaining the resolution required is appreciated! Thank you.

我有这个方法来计算一个NSNumber整数数组的标准偏差,给出一个平均值。计算使用NSDecimals保留最高分辨率。这目前要求许多cpu周期,任何帮助加快它,同时保持所需的分辨率是值得赞赏的!谢谢。

-(NSDecimal)standardDeviationOf:(NSMutableArray *)array withMean:(NSDecimal)mean {

if (![array count]) return CPTDecimalFromInt(0);

NSDecimal sumOfSquaredDifferences = CPTDecimalFromInt(0);


for (NSNumber *number in array) {

    NSDecimal valueOfNumber = CPTDecimalFromInt([number intValue]);
    NSDecimal difference = CPTDecimalSubtract(valueOfNumber, mean);
    sumOfSquaredDifferences = CPTDecimalAdd(sumOfSquaredDifferences, CPTDecimalMultiply(difference, difference));

}

return CPTDecimalFromDouble(
                            sqrt(
                                 CPTDecimalDoubleValue(sumOfSquaredDifferences) / [[NSNumber numberWithInt:[array count]] doubleValue]
                                 )
                            );
}

1 个解决方案

#1


0  

An NSDecimal has 38 digits of precision, whereas double has roughly 16 digits of precision. But at the end of your loop, when you convert sumOfSquaredDifferences to double for the sqrt function, all the extra precision you had in the NSDecimal is "lost". You might as well perform the arithmetic of your inner loop using double, which should be much faster than NSDecimal:

NSDecimal有38位精度,而double大约有16位精度。但是在循环结束时,当你将sumOfSquaredDifferences转换为sqrt函数的double时,你在NSDecimal中获得的所有额外精度都会“丢失”。您也可以使用double执行内部循环的算法,这应该比NSDecimal快得多:

double sumOfSquaredDifferences = 0;
double valueOfMean = [mean doubleValue];
for (NSNumber *number in array) {

    double valueOfNumber = [number intValue];
    double difference = valueOfNumber - valueOfMean;
    sumOfSquaredDifferences += difference * difference;

}

return CPTDecimalFromDouble(sqrt(sumOfSquaredDifferences /
                                 double([array count])));

#1


0  

An NSDecimal has 38 digits of precision, whereas double has roughly 16 digits of precision. But at the end of your loop, when you convert sumOfSquaredDifferences to double for the sqrt function, all the extra precision you had in the NSDecimal is "lost". You might as well perform the arithmetic of your inner loop using double, which should be much faster than NSDecimal:

NSDecimal有38位精度,而double大约有16位精度。但是在循环结束时,当你将sumOfSquaredDifferences转换为sqrt函数的double时,你在NSDecimal中获得的所有额外精度都会“丢失”。您也可以使用double执行内部循环的算法,这应该比NSDecimal快得多:

double sumOfSquaredDifferences = 0;
double valueOfMean = [mean doubleValue];
for (NSNumber *number in array) {

    double valueOfNumber = [number intValue];
    double difference = valueOfNumber - valueOfMean;
    sumOfSquaredDifferences += difference * difference;

}

return CPTDecimalFromDouble(sqrt(sumOfSquaredDifferences /
                                 double([array count])));

相关文章