fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <time.h>
  4.  
  5. double func(double x) {
  6. return x * sin(x * 0.5);
  7. }
  8.  
  9. int main() {
  10. double b = 3.1415926;
  11. const int n = 10000;
  12. const double h = b / n;
  13. const double c = 0.5 * h;
  14.  
  15. clock_t start, end;
  16. double total_time = 0.0;
  17.  
  18. for (int j = 0; j < 10000; j++) {
  19. double s = func(b);
  20. double x = h;
  21.  
  22. start = clock();
  23.  
  24. for (int i = 1; i < n; i += 2) {
  25. s += 2.0 * (func(x) + func(x + h));
  26. x += 2.0 * h;
  27. }
  28. s *= c;
  29.  
  30. end = clock();
  31. total_time += (double)(end - start) / CLOCKS_PER_SEC;
  32. }
  33.  
  34. printf("Average time: %lf seconds\n", total_time / 1000.0);
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Average time: 0.000001 seconds