Norvager

Test pow

Nov 10th, 2021
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define Inf 1000*1000*1000
  3. #include <iostream>
  4. #include <chrono>
  5. #include <ctime>
  6. #include <cmath>
  7. using namespace std;
  8. using namespace chrono;
  9.  
  10. int main() {
  11.     srand(static_cast<unsigned int>(time(0)));
  12.     double val;
  13.     time_point<high_resolution_clock> start, end;
  14.     start = high_resolution_clock::now();
  15.     for (int i = 0; i < Inf; i++) {
  16.         val = (double)rand() / RAND_MAX;
  17.         val = val * val;
  18.     }
  19.     end = high_resolution_clock::now();
  20.     duration<int64_t, nano> dur_ns = (end - start);
  21.     unsigned long long elapsed_seconds = dur_ns.count();
  22.     cout << "val*val: " << elapsed_seconds << endl;
  23.  
  24.     start = high_resolution_clock::now();
  25.     for (int i = 0; i < Inf; i++) {
  26.         val = (double)rand() / RAND_MAX;
  27.         val = val * val;
  28.     }
  29.     end = high_resolution_clock::now();
  30.     dur_ns = (end - start);
  31.     elapsed_seconds = dur_ns.count();
  32.     cout << "*=val: " << elapsed_seconds << endl;
  33.  
  34.     start = high_resolution_clock::now();
  35.     for (int i = 0; i < Inf; i++) {
  36.         val = (double)rand() / RAND_MAX;
  37.         val = pow(val, 2);
  38.     }
  39.     end = high_resolution_clock::now();
  40.     dur_ns = (end - start);
  41.     elapsed_seconds = dur_ns.count();
  42.     cout << "pow: " << elapsed_seconds << endl;
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment