John이 말한대로 소수 자릿수가있는 숫자는 기본값이 double입니다. TomTom이 잘못되었습니다.
컴파일러가 const float (두 가지 일이 일어난 것으로 가정)에 최적화되어 있는지 궁금 해서요 ... 속도 증가에 대한 아이디어가 실제로 합법적 인 것 같아요. 당신이 그것을 얼마나 많이 사용 하느냐에 달려 있습니다. 수학이 많이 필요한 응용 프로그램에서는 아마도이 트릭을 사용하고 싶을 것입니다.
float 변수를 저장하고 double로 캐스팅 한 다음 double (f가없는 숫자)에 대해 수학을 수행 한 다음 다시 float 변수로 캐스팅하여 다시 저장해야합니다. 매번 플로트에 저장하더라도 계산의 차이를 설명 할 수 있습니다.
코드 & 원료 결과 https://gist.github.com/1880400
은 (릴리스은 F 표기법을 사용하여 성능 증가의 더욱 결과) 디버그 프로필 아이 패드 (1)에 관련된 기준을 꺼냈다 :
------------ 10000000 total loops
timeWithDoubles: 1.33593 sec
timeWithFloats: 0.80924 sec
Float speed up: 1.65x
Difference in calculation: -0.000038
코드 :
int main (int argc, const char * argv[]) {
for (unsigned int magnitude = 100; magnitude < INT_MAX; magnitude *= 10) {
runTest(magnitude);
}
return 0;
}
void runTest(int numIterations) {
NSTimeInterval startTime = CFAbsoluteTimeGetCurrent();
float d = 1.2f;
for (int i = 0; i < numIterations; i++) {
d += 1.8368383;
d *= 0.976;
}
NSTimeInterval timeWithDoubles = CFAbsoluteTimeGetCurrent() - startTime;
startTime = CFAbsoluteTimeGetCurrent();
float f = 1.2f;
for (int i = 0; i < numIterations; i++) {
f += 1.8368383f;
f *= 0.976f;
}
NSTimeInterval timeWithFloats = CFAbsoluteTimeGetCurrent() - startTime;
printf("\n------------ %d total loops\n", numIterations);
printf("timeWithDoubles: %2.5f sec\n", timeWithDoubles);
printf("timeWithFloats: %2.5f sec\n", timeWithFloats);
printf("Float speed up: %2.2fx\n", timeWithDoubles/timeWithFloats);
printf("Difference in calculation: %f\n", d - f);
}
은 사실 "1.0"우스입니다 동맹국은 부유층이 아니라 이중으로 취급됩니다. –