strtof_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <stdarg.h>
  6. #include <unistd.h>
  7. #include <float.h>
  8. #include <math.h>
  9. float strtof_1(char* s, char** end);
  10. long cacheSize;
  11. long halfCacheSize;
  12. char* dest; // to help keep gcc from optimizing it away
  13. char* cachebuster;
  14. double getCurrentTime() { // in seconds
  15. double now;
  16. struct timespec ts;
  17. static double offset = 0;
  18. // CLOCK_MONOTONIC_RAW is linux-specific.
  19. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  20. now = (double)ts.tv_sec + ((double)ts.tv_nsec / 1000000000.0);
  21. if(offset == 0) offset = now;
  22. return now - offset;
  23. }
  24. // touch a large, contiguous section of memory that will overlap with the entire cache (hopefully, assuming there's no vma magic here)
  25. void reset_cache() {
  26. for(size_t i = 0; i < cacheSize; i++) {
  27. cachebuster[i]++;
  28. }
  29. }
  30. int main(int argc, char* argv[]) {
  31. srand(101);
  32. cacheSize = sysconf(_SC_LEVEL3_CACHE_SIZE);
  33. long l4 = sysconf(_SC_LEVEL4_CACHE_SIZE);
  34. if(l4 > cacheSize) cacheSize = l4;
  35. halfCacheSize = cacheSize / 2;
  36. cachebuster = calloc(1, cacheSize);
  37. fprintf(stderr, "cache size: %ld\n", cacheSize);
  38. /*
  39. char buf[300];
  40. for(unsigned long i = 0; i < 0xffffffff; i++) {
  41. union { unsigned int n; float f; } u;
  42. // u.n = i;
  43. u.n = rand();
  44. float o = u.f;
  45. // if(o >= 1 || o <= 0 || o < FLT_MIN) continue;
  46. //
  47. // for(unsigned long i = 0; i < 0xffffffff; i++) {
  48. // unsigned int u = rand();
  49. // float o = *(float*)&u;
  50. //
  51. if(!isfinite(o)) continue;
  52. // if(o >= 1 || o <= 0 || o < FLT_MIN) continue;
  53. // o = 3e-32f;
  54. // for(unsigned long i = 1; i < 10000; i++) {
  55. // float o = (float)i / 7;
  56. sprintf(buf, "%.9g", o); // %.9g for canonical representation
  57. // sprintf(buf, "%.70f", o); // %.9g for canonical representation
  58. float f = strtof_1(buf, NULL);
  59. if(f != o) {
  60. printf("%.9g [%s] != %.9g\n", o, buf, f);
  61. exit(1);
  62. }
  63. else {
  64. // printf("%.15f == %.15f\n", o, f);
  65. }
  66. if(i % 0xffffff == 0) printf("%ld\n", i);
  67. }
  68. exit(1);
  69. //*/
  70. int tries = 100000;
  71. char buf2[tries][70];
  72. for(unsigned long i = 0, n = 0; i < 0xffffffff && n < tries; i += 0xffff, n++) {
  73. union { unsigned int n; float f; } u;
  74. // u.n = i;
  75. u.n = rand();
  76. float o = u.f;
  77. if(!isfinite(o)) continue;
  78. n++;
  79. sprintf(buf2[n], "%.9g", o);
  80. // sprintf(buf2[n], "%.50f", o);
  81. // sprintf(buf2[n], "%.2f", o);
  82. }
  83. reset_cache();
  84. double start_mine = getCurrentTime();
  85. float sum = 0;
  86. for(int i = 0; i < tries; i++) {
  87. sum += strtof_1(buf2[i], NULL);
  88. }
  89. double end_mine = getCurrentTime();
  90. reset_cache();
  91. double start_libc = getCurrentTime();
  92. float sum2 = 0;
  93. for(int i = 0; i < tries; i++) {
  94. sum2 += strtof(buf2[i], NULL);
  95. }
  96. double end_libc = getCurrentTime();
  97. printf("libc: %.15f\n", end_libc - start_libc);
  98. printf("mine: %.15f\n", end_mine - start_mine);
  99. printf("%.9g, %.9g\r", sum, sum2);
  100. exit(0);
  101. }