Advertisement
Guest User

Untitled

a guest
Mar 24th, 2024
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.64 KB | Science | 0 0
  1. import std.stdio;
  2. import std.datetime.stopwatch;
  3. import std.math:pow;
  4.  
  5. const int ITERATIONS = 1_000_000;
  6. const int 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. void main() {
  17.     double result;
  18.     long total_time = 0;
  19.     for (int i = 0; i < BENCHMARKS; i++) {
  20.         auto sw = StopWatch(AutoStart.no);
  21.         sw.start();
  22.         result = leibniz(ITERATIONS);
  23.         sw.stop();
  24.         total_time += sw.peek.total!"nsecs";
  25.     }
  26.  
  27.     writefln("%f\n", result);
  28.     writefln("Avg execution time: %f\n", total_time / BENCHMARKS / 1e9);
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement