Created
October 23, 2016 12:20
-
-
Save msikma/baa515c4c4d752090485cf259661db63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile command: | |
// /usr/local/djgpp/bin/i586-pc-msdosdjgpp-gcc -O3 -c -o test.o test.c | |
// /usr/local/djgpp/bin/i586-pc-msdosdjgpp-gcc -O3 -o test.exe test.o | |
#include <stdio.h> | |
#include <time.h> | |
#include <math.h> | |
#include "src/utils/math.h" | |
// Sine lookup table for the range [0..360). | |
const float sin_table[] = { | |
0.0, | |
0.01745240643728351, 0.03489949670250097, 0.052335956242943835, | |
0.0697564737441253, 0.08715574274765817, 0.10452846326765347, | |
0.12186934340514748, 0.13917310096006544, 0.15643446504023087, | |
0.17364817766693033, 0.1908089953765448, 0.20791169081775934, | |
0.224951054343865, 0.24192189559966773, 0.25881904510252074, | |
0.27563735581699916, 0.29237170472273677, 0.3090169943749474, | |
0.3255681544571567, 0.3420201433256687, 0.35836794954530027, | |
0.374606593415912, 0.39073112848927377, 0.4067366430758002, | |
0.42261826174069944, 0.4383711467890774, 0.45399049973954675, | |
0.4694715627858908, 0.48480962024633706, | |
0.5, | |
0.5150380749100542, 0.5299192642332049, 0.5446390350150271, | |
0.5591929034707469, 0.573576436351046, 0.5877852522924731, | |
0.6018150231520483, 0.6156614753256583, 0.6293203910498374, | |
0.6427876096865393, 0.6560590289905073, 0.6691306063588582, | |
0.6819983600624985, 0.6946583704589973, 0.7071067811865475, | |
0.7193398003386511, 0.7313537016191705, 0.7431448254773942, | |
0.7547095802227719, 0.766044443118978, 0.7771459614569708, | |
0.7880107536067219, 0.7986355100472928, 0.8090169943749473, | |
0.8191520442889917, 0.8290375725550416, 0.838670567945424, | |
0.848048096156426, 0.8571673007021123, 0.8660254037844386, | |
0.8746197071393957, 0.8829475928589269, 0.8910065241883678, | |
0.898794046299167, 0.9063077870366499, 0.9135454576426009, | |
0.9205048534524404, 0.9271838545667874, 0.9335804264972017, | |
0.9396926207859083, 0.9455185755993167, 0.9510565162951535, | |
0.9563047559630354, 0.9612616959383189, 0.9659258262890683, | |
0.9702957262759965, 0.9743700647852352, 0.9781476007338056, | |
0.981627183447664, 0.984807753012208, 0.9876883405951378, | |
0.9902680687415704, 0.992546151641322, 0.9945218953682733, | |
0.9961946980917455, 0.9975640502598242, 0.9986295347545738, | |
0.9993908270190958, 0.9998476951563913, | |
1.0, | |
0.9998476951563913, 0.9993908270190958, 0.9986295347545738, | |
0.9975640502598242, 0.9961946980917455, 0.9945218953682733, | |
0.9925461516413221, 0.9902680687415704, 0.9876883405951378, | |
0.984807753012208, 0.981627183447664, 0.9781476007338057, | |
0.9743700647852352, 0.9702957262759965, 0.9659258262890683, | |
0.9612616959383189, 0.9563047559630355, 0.9510565162951536, | |
0.9455185755993168, 0.9396926207859084, 0.9335804264972017, | |
0.9271838545667874, 0.9205048534524403, 0.9135454576426009, | |
0.90630778703665, 0.8987940462991669, 0.8910065241883679, | |
0.8829475928589269, 0.8746197071393959, 0.8660254037844388, | |
0.8571673007021123, 0.8480480961564261, 0.838670567945424, | |
0.8290375725550418, 0.8191520442889917, 0.8090169943749475, | |
0.7986355100472928, 0.788010753606722, 0.7771459614569711, | |
0.7660444431189781, 0.7547095802227721, 0.7431448254773942, | |
0.7313537016191707, 0.7193398003386511, 0.7071067811865476, | |
0.6946583704589971, 0.6819983600624985, 0.6691306063588583, | |
0.6560590289905073, 0.6427876096865395, 0.6293203910498374, | |
0.6156614753256584, 0.6018150231520482, 0.5877852522924732, | |
0.5735764363510459, 0.5591929034707469, 0.5446390350150273, | |
0.5299192642332049, 0.5150380749100544, | |
0.5, | |
0.48480962024633717, 0.4694715627858907, 0.45399049973954686, | |
0.4383711467890773, 0.4226182617406995, 0.40673664307580043, | |
0.39073112848927377, 0.37460659341591224, 0.3583679495453002, | |
0.3420201433256689, 0.3255681544571566, 0.3090169943749475, | |
0.2923717047227366, 0.2756373558169992, 0.258819045102521, | |
0.24192189559966773, 0.2249510543438652, 0.2079116908177593, | |
0.19080899537654497, 0.17364817766693025, 0.15643446504023098, | |
0.1391731009600653, 0.12186934340514755, 0.10452846326765373, | |
0.0871557427476582, 0.06975647374412552, 0.05233595624294381, | |
0.03489949670250114, 0.017452406437283435, | |
0.0, | |
-0.01745240643728351, -0.03489949670250097, -0.052335956242943835, | |
-0.0697564737441253, -0.08715574274765817, -0.10452846326765347, | |
-0.12186934340514748, -0.13917310096006544, -0.15643446504023087, | |
-0.17364817766693033, -0.1908089953765448, -0.20791169081775934, | |
-0.224951054343865, -0.24192189559966773, -0.25881904510252074, | |
-0.27563735581699916, -0.29237170472273677, -0.3090169943749474, | |
-0.3255681544571567, -0.3420201433256687, -0.35836794954530027, | |
-0.374606593415912, -0.39073112848927377, -0.4067366430758002, | |
-0.42261826174069944, -0.4383711467890774, -0.45399049973954675, | |
-0.4694715627858908, -0.48480962024633706, | |
-0.5, | |
-0.5150380749100542, -0.5299192642332049, -0.5446390350150271, | |
-0.5591929034707469, -0.573576436351046, -0.5877852522924731, | |
-0.6018150231520483, -0.6156614753256583, -0.6293203910498374, | |
-0.6427876096865393, -0.6560590289905073, -0.6691306063588582, | |
-0.6819983600624985, -0.6946583704589973, -0.7071067811865475, | |
-0.7193398003386511, -0.7313537016191705, -0.7431448254773942, | |
-0.7547095802227719, -0.766044443118978, -0.7771459614569708, | |
-0.7880107536067219, -0.7986355100472928, -0.8090169943749473, | |
-0.8191520442889917, -0.8290375725550416, -0.838670567945424, | |
-0.848048096156426, -0.8571673007021123, -0.8660254037844386, | |
-0.8746197071393957, -0.8829475928589269, -0.8910065241883678, | |
-0.898794046299167, -0.9063077870366499, -0.9135454576426009, | |
-0.9205048534524404, -0.9271838545667874, -0.9335804264972017, | |
-0.9396926207859083, -0.9455185755993167, -0.9510565162951535, | |
-0.9563047559630354, -0.9612616959383189, -0.9659258262890683, | |
-0.9702957262759965, -0.9743700647852352, -0.9781476007338056, | |
-0.981627183447664, -0.984807753012208, -0.9876883405951378, | |
-0.9902680687415704, -0.992546151641322, -0.9945218953682733, | |
-0.9961946980917455, -0.9975640502598242, -0.9986295347545738, | |
-0.9993908270190958, -0.9998476951563913, | |
-1.0, | |
-0.9998476951563913, -0.9993908270190958, -0.9986295347545738, | |
-0.9975640502598242, -0.9961946980917455, -0.9945218953682733, | |
-0.9925461516413221, -0.9902680687415704, -0.9876883405951378, | |
-0.984807753012208, -0.981627183447664, -0.9781476007338057, | |
-0.9743700647852352, -0.9702957262759965, -0.9659258262890683, | |
-0.9612616959383189, -0.9563047559630355, -0.9510565162951536, | |
-0.9455185755993168, -0.9396926207859084, -0.9335804264972017, | |
-0.9271838545667874, -0.9205048534524403, -0.9135454576426009, | |
-0.90630778703665, -0.8987940462991669, -0.8910065241883679, | |
-0.8829475928589269, -0.8746197071393959, -0.8660254037844388, | |
-0.8571673007021123, -0.8480480961564261, -0.838670567945424, | |
-0.8290375725550418, -0.8191520442889917, -0.8090169943749475, | |
-0.7986355100472928, -0.788010753606722, -0.7771459614569711, | |
-0.7660444431189781, -0.7547095802227721, -0.7431448254773942, | |
-0.7313537016191707, -0.7193398003386511, -0.7071067811865476, | |
-0.6946583704589971, -0.6819983600624985, -0.6691306063588583, | |
-0.6560590289905073, -0.6427876096865395, -0.6293203910498374, | |
-0.6156614753256584, -0.6018150231520482, -0.5877852522924732, | |
-0.5735764363510459, -0.5591929034707469, -0.5446390350150273, | |
-0.5299192642332049, -0.5150380749100544, | |
-0.5, | |
-0.48480962024633717, -0.4694715627858907, -0.45399049973954686, | |
-0.4383711467890773, -0.4226182617406995, -0.40673664307580043, | |
-0.39073112848927377, -0.37460659341591224, -0.3583679495453002, | |
-0.3420201433256689, -0.3255681544571566, -0.3090169943749475, | |
-0.2923717047227366, -0.2756373558169992, -0.258819045102521, | |
-0.24192189559966773, -0.2249510543438652, -0.2079116908177593, | |
-0.19080899537654497, -0.17364817766693025, -0.15643446504023098, | |
-0.1391731009600653, -0.12186934340514755, -0.10452846326765373, | |
-0.0871557427476582, -0.06975647374412552, -0.05233595624294381, | |
-0.03489949670250114, -0.017452406437283435 | |
}; | |
// Retrieves value from the lookup table. | |
extern inline float degsin(int deg); | |
inline float degsin(int deg) { | |
return sin_table[deg]; | |
} | |
// Precomputed float values that we'll pass to sin() for testing. | |
float vals[360]; | |
int main(int argc, char *argv[]) { | |
int a, b; | |
clock_t start, diff; | |
float result_a = 0.0, result_b = 0.0; | |
printf("Speed test for sin() versus a lookup table sin().\r\n"); | |
printf("Getting ready for sin() test...\r\n"); | |
// Precompute sin() values. | |
for (int b = 0; b < 360; ++b) { | |
vals[b] = b * (3.14159265359 / 180); | |
} | |
// Warming up... | |
for (int a = 0; a < 10000; ++a) { | |
for (int b = 0; b < 360; ++b) { | |
result_a += sin(vals[b]); | |
} | |
} | |
for (int a = 0; a < 10000; ++a) { | |
for (int b = 0; b < 360; ++b) { | |
result_a += sin(vals[b]); | |
} | |
} | |
// sin() test. | |
start = clock(); | |
printf("Start sin() test..."); | |
for (int a = 0; a < 10000; ++a) { | |
for (int b = 0; b < 360; ++b) { | |
result_a += sin(vals[b]); | |
} | |
} | |
printf(" done.\r\n"); | |
diff = clock() - start; | |
printf("10,000 iterations of 360 degrees took %d msec with sin().\r\n", diff * 1000 / CLOCKS_PER_SEC); | |
// Warming up for the lookup table test. | |
for (int a = 0; a < 10000; ++a) { | |
for (int b = 0; b < 360; ++b) { | |
result_b += degsin(b); | |
} | |
} | |
for (int a = 0; a < 10000; ++a) { | |
for (int b = 0; b < 360; ++b) { | |
result_b += degsin(b); | |
} | |
} | |
// Lookup table test. | |
start = clock(); | |
printf("Start degsin() test..."); | |
for (int a = 0; a < 10000; ++a) { | |
for (int b = 0; b < 360; ++b) { | |
result_b += degsin(b); | |
} | |
} | |
printf(" done.\r\n"); | |
diff = clock() - start; | |
printf("10,000 iterations of 360 degrees took %d msec with degsin().\r\n", diff * 1000 / CLOCKS_PER_SEC); | |
printf("%f, %f", result_a, result_b); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result on Pentium 166mhz: