-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
59 lines (51 loc) · 1.86 KB
/
main.zig
File metadata and controls
59 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
// Get the number of rounds (cross-platform).
const strRounds = std.process.getEnvVarOwned(allocator, "rounds") catch |err| {
if (err == error.EnvironmentVariableNotFound) {
try stdout.print("*** The 'rounds' environment variable is not set\n", .{});
} else {
try stdout.print("*** Error reading 'rounds' environment variable\n", .{});
}
try stdout.flush();
std.process.exit(1);
};
defer allocator.free(strRounds);
const rounds = std.fmt.parseInt(i64, strRounds, 10) catch {
try stdout.print("*** The 'rounds' environment variable must be an integer, saw: {s}\n", .{strRounds});
try stdout.flush();
std.process.exit(1);
};
var sum: f64 = 0.0;
var flip: f64 = -1.0;
var pi: f64 = undefined;
var t_start: i64 = undefined;
var t_stop: i64 = undefined;
// Prime the caches.
var ix: i64 = 1;
while (ix <= 3) : (ix += 1) {
flip *= -1.0;
sum += flip / @as(f64, @floatFromInt(ix + ix - 1));
}
sum = 0.0;
flip = -1.0;
// Timed test.
t_start = std.time.milliTimestamp();
ix = 1;
while (ix <= rounds) : (ix += 1) {
flip *= -1.0;
sum += flip / @as(f64, @floatFromInt(ix + ix - 1));
}
pi = sum * 4.0;
t_stop = std.time.milliTimestamp();
// Report.
const t_delta_secs = @as(f64, @floatFromInt(t_stop - t_start)) / 1000.0;
try stdout.print("Zig,{d},{d:.3},{d:.40}\n", .{rounds, t_delta_secs, pi});
try stdout.flush();
}