Advertisement
Guest User

Untitled

a guest
Apr 10th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. /***************** SYSTEM ************************
  2. ******** Cpu: Intel Q6600 ************************
  3. ******** O.S.: Windows 7 x64 *********************
  4. ******** Compiler: Visual Studio 2012 (C++11) ****
  5. ******** Optimizations disabled ******************
  6. ****************** OUTPUT ************************
  7. ******** Benchmark uintSqrt(): 1326 ticks ********
  8. ******** Benchmark 1.0/Q_rsqrt(): 546 ticks ******
  9. ******** Benchmark sqrt(): 640 ticks *************
  10. ******** Benchmark pow(): 1139 ticks *************
  11. **************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <Windows.h>
  16. #include <math.h>
  17.  
  18. typedef unsigned int uint32_t;
  19.  
  20. // Calcola la radice quadrata di un numero intero senza segno
  21. uint32_t uintSqrt(uint32_t n)
  22. {
  23.     register uint32_t i;
  24.     register uint32_t val;
  25.      
  26.     // se n vale 0 ritorna 0
  27.     if (!n) return 0;
  28.      
  29.     // calcola il numero di bit di n
  30.     i = 0;
  31.     val = n;
  32.     do
  33.     {
  34.         val = val >> 1;
  35.         if (val) i++;
  36.     } while (val);
  37.      
  38.     // assume val come 2^(i/2)
  39.     if (!i) val=1; else val = 1 << (i>>1);
  40.      
  41.     // Calcola la radice babilonese.
  42.     // 4 passaggi sono sufficienti
  43.     for(i=0; i<4; i++) val = (val + n/val) >> 1;
  44.      
  45.     return val;
  46. }
  47.  
  48. float Q_rsqrt( float number )
  49. {
  50.     register long i;
  51.     register float x2, y;
  52.     const float threehalfs = 1.5F;
  53.  
  54.     x2 = number * 0.5F;
  55.     y  = number;
  56.     i  = * ( long * ) &y;
  57.     i  = 0x5f3759df - ( i >> 1 );
  58.     y  = * ( float * ) &i;
  59.     y  = y * ( threehalfs - ( x2 * y * y ) );
  60.     return y;
  61. }
  62.  
  63. int main()
  64. {
  65.     srand(time(0));
  66.     auto ticks = GetTickCount();
  67.     for (unsigned int i = 0; i < 10000000; i++) {
  68.         register unsigned int f = uintSqrt((unsigned int)(rand()%20000));
  69.     }
  70.     ticks = GetTickCount() - ticks;
  71.     printf("Benchmark uintSqrt(): %u ticks\n", ticks);
  72.  
  73.     ticks = GetTickCount();
  74.     for (unsigned int i = 0; i < 10000000; i++) {
  75.         register float f = 1.0f/Q_rsqrt((float)(rand()%20000));
  76.     }
  77.     ticks = GetTickCount() - ticks;
  78.     printf("Benchmark 1.0/Q_rsqrt(): %u ticks\n", ticks);
  79.  
  80.     ticks = GetTickCount();
  81.     for (unsigned int i = 0; i < 10000000; i++) {
  82.         register float f = sqrt((float)(rand()%20000));
  83.     }
  84.     ticks = GetTickCount() - ticks;
  85.     printf("Benchmark sqrt(): %u ticks\n", ticks);
  86.  
  87.     ticks = GetTickCount();
  88.     for (unsigned int i = 0; i < 10000000; i++) {
  89.         register float f = pow((float)(rand()%20000), 0.5f);
  90.     }
  91.     ticks = GetTickCount() - ticks;
  92.     printf("Benchmark pow(): %u ticks\n", ticks);
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement