Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.datetime.stopwatch;
- import std.math:pow;
- const int ITERATIONS = 1_000_000;
- const int BENCHMARKS = 20;
- double leibniz(int iter) {
- double n = 1.0;
- for (int i = 2; i <= iter - 1; i++) {
- n += pow(-1.0, i - 1.0) / (i * 2.0 - 1.0);
- }
- return n * 4.0;
- }
- void main() {
- double result;
- long total_time = 0;
- for (int i = 0; i < BENCHMARKS; i++) {
- auto sw = StopWatch(AutoStart.no);
- sw.start();
- result = leibniz(ITERATIONS);
- sw.stop();
- total_time += sw.peek.total!"nsecs";
- }
- writefln("%f\n", result);
- writefln("Avg execution time: %f\n", total_time / BENCHMARKS / 1e9);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement