Skip to content

Instantly share code, notes, and snippets.

@azinman
Created February 22, 2012 01:17
Show Gist options
  • Save azinman/1880400 to your computer and use it in GitHub Desktop.
Save azinman/1880400 to your computer and use it in GitHub Desktop.
Comparison of float and double pre-defined values in iOS/LLVM
//
// main.m
// quicky
//
// Created by Aaron Zinman on 2/21/12.
// Copyright (c) 2012 Empirical Design LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
void runTest(int numIterations);
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);
}
/*
************* IN SIMULATOR, DEBUG *************
------------ 100 total loops
timeWithDoubles: 0.00000 sec
timeWithFloats: 0.00000 sec
Float speed up: 1.06x
Difference in calculation: -0.000031
------------ 1000 total loops
timeWithDoubles: 0.00001 sec
timeWithFloats: 0.00001 sec
Float speed up: 1.50x
Difference in calculation: -0.000038
------------ 10000 total loops
timeWithDoubles: 0.00012 sec
timeWithFloats: 0.00008 sec
Float speed up: 1.60x
Difference in calculation: -0.000038
------------ 100000 total loops
timeWithDoubles: 0.00123 sec
timeWithFloats: 0.00073 sec
Float speed up: 1.70x
Difference in calculation: -0.000038
------------ 1000000 total loops
timeWithDoubles: 0.01207 sec
timeWithFloats: 0.00749 sec
Float speed up: 1.61x
Difference in calculation: -0.000038
------------ 10000000 total loops
timeWithDoubles: 0.11871 sec
timeWithFloats: 0.07306 sec
Float speed up: 1.62x
Difference in calculation: -0.000038
------------ 100000000 total loops
timeWithDoubles: 1.16302 sec
timeWithFloats: 0.72541 sec
Float speed up: 1.60x
Difference in calculation: -0.000038
************* ON IPAD 1, DEBUG *************
------------ 100 total loops
timeWithDoubles: 0.00002 sec
timeWithFloats: 0.00001 sec
Float speed up: 1.51x
Difference in calculation: -0.000031
------------ 1000 total loops
timeWithDoubles: 0.00013 sec
timeWithFloats: 0.00008 sec
Float speed up: 1.58x
Difference in calculation: -0.000038
------------ 10000 total loops
timeWithDoubles: 0.00125 sec
timeWithFloats: 0.00079 sec
Float speed up: 1.58x
Difference in calculation: -0.000038
------------ 100000 total loops
timeWithDoubles: 0.01331 sec
timeWithFloats: 0.00800 sec
Float speed up: 1.66x
Difference in calculation: -0.000038
------------ 1000000 total loops
timeWithDoubles: 0.12797 sec
timeWithFloats: 0.08504 sec
Float speed up: 1.50x
Difference in calculation: -0.000038
------------ 10000000 total loops
timeWithDoubles: 1.33593 sec
timeWithFloats: 0.80924 sec
Float speed up: 1.65x
Difference in calculation: -0.000038
************* IN SIMULATOR, RELEASE (-O3) *************
------------ 100 total loops
timeWithDoubles: 0.00000 sec
timeWithFloats: 0.00000 sec
Float speed up: infx
Difference in calculation: -0.000031
------------ 1000 total loops
timeWithDoubles: 0.00001 sec
timeWithFloats: 0.00000 sec
Float speed up: 1.76x
Difference in calculation: -0.000038
------------ 10000 total loops
timeWithDoubles: 0.00008 sec
timeWithFloats: 0.00003 sec
Float speed up: 2.48x
Difference in calculation: -0.000038
------------ 100000 total loops
timeWithDoubles: 0.00077 sec
timeWithFloats: 0.00031 sec
Float speed up: 2.47x
Difference in calculation: -0.000038
------------ 1000000 total loops
timeWithDoubles: 0.00774 sec
timeWithFloats: 0.00310 sec
Float speed up: 2.49x
Difference in calculation: -0.000038
------------ 10000000 total loops
timeWithDoubles: 0.07770 sec
timeWithFloats: 0.03098 sec
Float speed up: 2.51x
Difference in calculation: -0.000038
------------ 100000000 total loops
timeWithDoubles: 0.77059 sec
timeWithFloats: 0.30755 sec
Float speed up: 2.51x
Difference in calculation: -0.000038
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment