fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <random>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7. const double PI = 3.141592653589793;
  8. const double PI2 = 2.0 * 3.141592653589793;
  9. const double PI4 = 4.0 * 3.141592653589793;
  10. const float PIf = 3.141592653589793;
  11. const float PI2f = 2.0 * 3.141592653589793;
  12. const float PI4f = 4.0 * 3.141592653589793;
  13.  
  14. const int N = 14400;
  15. const int HALF_N = N >> 1;
  16. const double STEP = 4.0 * PI / (double)N;
  17. const double INV_STEP = 1.0 / STEP;
  18. static double LUT[N * 2];
  19.  
  20. const int Nsm = 90;
  21. const int HALF_Nsm = Nsm >> 1;
  22. const float STEPsm = 4.0 * PIf / (float)Nsm;
  23. const float INV_STEPsm = 1.0 / STEPsm;
  24. static float LUTsm[Nsm * 2];
  25.  
  26. int seed = (int)clock();
  27.  
  28. void benchLUT() {
  29. cout << "LUT (double)" << endl;
  30. srand(seed);
  31. double result = 0.0;
  32. clock_t t;
  33. t = clock();
  34. for(int x = 0; x < 100000; x++) {
  35. double r = PI4 * ((double)rand() / RAND_MAX) - PI2;
  36. int index = (int)((r * INV_STEP) + HALF_N) << 1;
  37. result += LUT[index] + LUT[index + 1];
  38. }
  39. t = clock() - t;
  40. cout << "Sum : " << result << endl;
  41. cout << "Time : " << t << " : " << ((float)t)/CLOCKS_PER_SEC << endl;
  42. }
  43. void benchSinCos() {
  44. cout << "CMATH (float)" << endl;
  45. srand(seed);
  46. float result = 0.0;
  47. clock_t t;
  48. t = clock();
  49. for(int x = 0; x < 100000; x++) {
  50. float r = (float)(PI4f * ((float)rand() / RAND_MAX) - PI2f);
  51. result += sin(r) + cos(r);
  52. }
  53. t = clock() - t;
  54. cout << "Sum : " << result << endl;
  55. cout << "Time : " << t << " : " << ((float)t)/CLOCKS_PER_SEC << endl;
  56. }
  57. void benchPoly() {
  58. cout << "Poly (float)" << endl;
  59. srand(seed);
  60. float result = 0.0;
  61. clock_t t;
  62. t = clock();
  63. for(int x = 0; x < 100000; x++) {
  64. float r = (float)(PI4f * ((float)rand() / RAND_MAX) - PI2f);
  65. // wrap
  66. if (r < -3.14159265) {
  67. r += 6.28318531;
  68. } else if (r > 3.14159265) {
  69. r -= 6.28318531;
  70. }
  71. // sin
  72. if (r < 0) {
  73. result += 1.27323954 * r + 0.405284735 * r * r;
  74. } else {
  75. result += 1.27323954 * r - 0.405284735 * r * r;
  76. }
  77. // cos
  78. r += 1.57079632;
  79. if (r > 3.14159265) {
  80. r -= 6.28318531;
  81. }
  82. if (r < 0) {
  83. result += 1.27323954 * r + 0.405284735 * r * r;
  84. } else {
  85. result += 1.27323954 * r - 0.405284735 * r * r;
  86. }
  87. //result = sin(r) + cos(r);
  88. }
  89. t = clock() - t;
  90. cout << "Sum : " << result << endl;
  91. cout << "Time : " << t << " : " << ((float)t)/CLOCKS_PER_SEC << endl;
  92. }
  93. void benchInterp() {
  94. cout << "Small LUT Interp (float)" << endl;
  95. srand(seed);
  96. float result = 0.0;
  97. clock_t t;
  98. t = clock();
  99. for(int x = 0; x < 100000; x++) {
  100. float r = PI4f * ((float)rand() / RAND_MAX) - PI2f;
  101. int index = (int)((r * INV_STEPsm) + HALF_Nsm) << 1;
  102. result += LUTsm[index] + LUTsm[index + 1];
  103. }
  104. t = clock() - t;
  105. cout << "Sum : " << result << endl;
  106. cout << "Time : " << t << " : " << ((float)t)/CLOCKS_PER_SEC << endl;
  107. }
  108.  
  109. int main() {
  110. // build LUT
  111. int i = 0;
  112. double r = -2.0 * PI;
  113. for(i=0; i < N; i++, r += STEP) {
  114. LUT[(i << 1)] = sin(r);
  115. LUT[(i << 1) + 1] = cos(r);
  116. }
  117. // build small LUT
  118. r = -2.0 * PIf;
  119. for(i=0; i < Nsm; i++, r += STEPsm) {
  120. LUTsm[(i << 1)] = sin(r);
  121. LUTsm[(i << 1) + 1] = cos(r);
  122. }
  123.  
  124. benchLUT();
  125. benchSinCos();
  126. benchPoly();
  127. benchInterp();
  128. benchLUT();
  129. benchSinCos();
  130. benchPoly();
  131. benchInterp();
  132. return 0;
  133. }
Success #stdin #stdout 0.06s 3640KB
stdin
1
2
10
42
11
stdout
LUT (double)
Sum : 125.021
Time : 5692 : 0.005692
CMATH (float)
Sum : 125.014
Time : 13911 : 0.013911
Poly (float)
Sum : 138.512
Time : 7846 : 0.007846
Small LUT Interp (float)
Sum : 120.153
Time : 5019 : 0.005019
LUT (double)
Sum : 125.021
Time : 6123 : 0.006123
CMATH (float)
Sum : 125.014
Time : 13029 : 0.013029
Poly (float)
Sum : 138.512
Time : 7528 : 0.007528
Small LUT Interp (float)
Sum : 120.153
Time : 4647 : 0.004647