Advertisement
runewalsh

float vs double

Jun 9th, 2014
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. // floats: avg. 0.76 ms
  2. // doubles: avg. 0.70 ms
  3.  
  4. #include <chrono>
  5. using std::chrono::time_point;
  6. using std::chrono::steady_clock;
  7. using std::chrono::duration_cast;
  8. using std::chrono::nanoseconds;
  9.  
  10. inline float osqrt(float x) { return sqrtf(x); }
  11. inline double osqrt(double x) { return sqrt(x); }
  12.  
  13. template<typename real> void test(const char* realname)
  14. {
  15.     const int Iterations = 3500;
  16.     const int ArraySize = 100000;
  17.  
  18.     real ary[ArraySize];
  19.     real dummy = 0.0;
  20.     for (int i = 0; i < ArraySize; i++)
  21.         ary[i] = (real)rand();
  22.  
  23.     auto a = steady_clock::now();
  24.     for (int it = 0; it < Iterations; it++)
  25.         for (int i = 0; i < ArraySize; i++)
  26.         {
  27.             ary[i] = osqrt(ary[i]);
  28.             dummy += ary[i];
  29.         }
  30.     auto dur = steady_clock::now() - a;
  31.  
  32.     printf("%ss: avg. %.2f ms\n", realname, (double)duration_cast<nanoseconds>(dur).count() / (double)Iterations * 1.0e-6, dummy);
  33. }
  34.  
  35. int main(int argc, char* argv[])
  36. {
  37.     test<float>("float");
  38.     test<double>("double");
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement