Advertisement
Guest User

Untitled

a guest
Mar 24th, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | Science | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <time.h>
  4.  
  5. #define ITERATIONS 1000000
  6. #define BENCHMARKS 20
  7.  
  8. double leibniz(int iter) {
  9.   double n = 1.0;
  10.   for (int i = 2; i <= iter - 1; i++) {
  11.     n += pow(-1.0, i - 1.0) / (i * 2.0 - 1.0);
  12.   }
  13.   return n * 4.0;
  14. }
  15.  
  16. int main() {
  17.     double result;
  18.     double total_time = 0;
  19.     clock_t begin, end;
  20.     for (int i = 0; i < BENCHMARKS; i++) {
  21.         begin = clock();
  22.         result = leibniz(ITERATIONS);
  23.         end = clock();
  24.         total_time += (double)(end - begin);
  25.     }
  26.  
  27.     printf("%8lf\n", result);
  28.     printf("Avg execution time: %8lf\n", total_time / BENCHMARKS / CLOCKS_PER_SEC);
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement