Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions bench/algorithm/binarytrees/1.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const print = @import("print.zig");
const builtin = @import("builtin");
const math = std.math;
const Allocator = std.mem.Allocator;
Expand All @@ -7,14 +8,14 @@ const MIN_DEPTH = 4;
const global_allocator = std.heap.c_allocator;

pub fn main() !void {
const stdout = std.io.getStdOut().writer();
// Zig 0.15: avoid std.io stdout writer; use a small fmt+write helper to stdout.
const n = try get_n();
const max_depth = @max(MIN_DEPTH + 2, n);
{
const stretch_depth = max_depth + 1;
const stretch_tree = Node.make(stretch_depth, global_allocator).?;
defer stretch_tree.deinit();
try stdout.print("stretch tree of depth {d}\t check: {d}\n", .{ stretch_depth, stretch_tree.check() });
try print.printFmt("stretch tree of depth {d}\t check: {d}\n", .{ stretch_depth, stretch_tree.check() });
}
const long_lived_tree = Node.make(max_depth, global_allocator).?;
defer long_lived_tree.deinit();
Expand All @@ -29,17 +30,17 @@ pub fn main() !void {
defer tree.deinit();
sum += tree.check();
}
try stdout.print("{d}\t trees of depth {d}\t check: {d}\n", .{ iterations, depth, sum });
try print.printFmt("{d}\t trees of depth {d}\t check: {d}\n", .{ iterations, depth, sum });
}

try stdout.print("long lived tree of depth {d}\t check: {d}\n", .{ max_depth, long_lived_tree.check() });
try print.printFmt("long lived tree of depth {d}\t check: {d}\n", .{ max_depth, long_lived_tree.check() });
}

fn get_n() !usize {
var arg_it = std.process.args();
_ = arg_it.skip();
const arg = arg_it.next() orelse return 10;
return try std.fmt.parseInt(u32, arg, 10);
return @as(usize, @intCast(try std.fmt.parseInt(u32, arg, 10)));
}

const Node = struct {
Expand Down
5 changes: 3 additions & 2 deletions bench/algorithm/coro-prime-sieve/1.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// https://gist.github.com/SpexGuy/953e5780cd2d2c524cba6a79f13076e6

const std = @import("std");
const print = @import("print.zig");

const Channel = struct {
value: u32,
Expand Down Expand Up @@ -34,7 +35,7 @@ fn filter(out_channel: *Channel, in_channel: *Channel, prime: u32) void {

var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const global_allocator = arena.allocator();
const stdout = std.io.getStdOut().writer();


pub fn main() !void {
defer arena.deinit();
Expand All @@ -52,7 +53,7 @@ pub fn main() !void {
while (i < n) : (i += 1) {
resume ch.frame;
const prime = ch.value;
try stdout.print("{}\n", .{prime});
try print.printFmt("{}\n", .{prime});
if (i >= n - 1) break;
const ch1 = try global_allocator.create(Channel);
const frame = try global_allocator.create(@Frame(filter));
Expand Down
7 changes: 4 additions & 3 deletions bench/algorithm/edigits/1.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const print = @import("print.zig");
const bigint = std.math.big.int;
const math = std.math;
const global_allocator = std.heap.c_allocator;
Expand All @@ -13,7 +14,6 @@ const Pair = struct {
};

pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const n = try get_n();
const k = binary_search(n);
var pair = try sum_terms(0, k - 1);
Expand Down Expand Up @@ -44,14 +44,15 @@ pub fn main() !void {
j += 1;
}
if (i + 10 <= n_usize) {
try stdout.print("{s}\t:{d}\n", .{ sb, i + 10 });
try print.printFmt("{s}\t:{d}\n", .{ sb, i + 10 });
} else {
try stdout.print("{s}\t:{d}\n", .{ sb, n });
try print.printFmt("{s}\t:{d}\n", .{ sb, n });
}
i += 10;
}
}


fn sum_terms(a: i32, b: i32) anyerror!Pair {
if (b == a + 1) {
const p = try bigint.Managed.initSet(global_allocator, 1);
Expand Down
5 changes: 3 additions & 2 deletions bench/algorithm/fannkuch-redux/1.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// From https://github.com/tiehuis/zig-benchmarks-game/blob/master/src/fannkuch-redux.zig

const std = @import("std");
const print = @import("print.zig");

const global_allocator = std.heap.c_allocator;

var buffer: [1024]u8 = undefined;
var fixed_allocator = std.heap.FixedBufferAllocator.init(buffer[0..]);
var allocator = fixed_allocator.allocator();


pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const n = try get_n();

var perm = try allocator.alloc(usize, n);
Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn main() !void {
}
}

try stdout.print("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flips_count });
try print.printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flips_count });
}

fn get_n() !usize {
Expand Down
5 changes: 3 additions & 2 deletions bench/algorithm/fannkuch-redux/2-m.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const print = @import("print.zig");

const max_n = 12;
const Vec = @Vector(max_n, u8);
Expand Down Expand Up @@ -128,10 +129,10 @@ pub fn main() !void {
const perms_count = factorialComptime(n);
try runInParallel(tasks, perms_count, pfannkuchenStats, .{ n, &stats });

const stdout = std.io.getStdOut().writer();
try stdout.print("{d}\nPfannkuchen({d}) = {d}\n", .{ stats.checksum, n, stats.max_flips });
try print.printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ stats.checksum, n, stats.max_flips });
}


fn get_n() !u8 {
var arg_it = std.process.args();
_ = arg_it.skip();
Expand Down
7 changes: 4 additions & 3 deletions bench/algorithm/fannkuch-redux/2.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const print = @import("print.zig");

const Vec = @Vector(16, u8);

Expand All @@ -25,7 +26,7 @@ fn next_perm_mask(n: u8) Vec {
}

fn apply_mask(a: Vec, n: u8, comptime mask: anytype) Vec {
const len = @typeInfo(Vec).Vector.len;
const len = @typeInfo(Vec).vector.len;
comptime var i: u8 = 0;
inline while (i < len) : (i += 1) if (i == n) return @shuffle(u8, a, undefined, mask(i));
unreachable;
Expand Down Expand Up @@ -62,10 +63,10 @@ pub fn main() !void {
for (count[1..r], 0..) |*v, i| v.* = @intCast(i + 2);
}

const stdout = std.io.getStdOut().writer();
try stdout.print("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flip_count });
try print.printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flip_count });
}


fn get_n() !u8 {
var arg_it = std.process.args();
_ = arg_it.skip();
Expand Down
6 changes: 4 additions & 2 deletions bench/algorithm/fannkuch-redux/3-i.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
//

const std = @import("std");
const print = @import("print.zig");


const u8x16 = @Vector(16, u8);

fn get_n() !u4 {
Expand All @@ -35,8 +38,7 @@ fn get_n() !u4 {
pub fn main() !void {
const n = try get_n();
const x = fannkuchRedux(n);
const stdout = std.io.getStdOut().writer();
try stdout.print("{}\nPfannkuchen({}) = {}\n", .{ x[0], n, x[1] });
try print.printFmt("{}\nPfannkuchen({}) = {}\n", .{ x[0], n, x[1] });
}

inline fn shuffle_epi8(x: u8x16, mask: u8x16) u8x16 {
Expand Down
21 changes: 12 additions & 9 deletions bench/algorithm/fasta/1.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn repeatAndWrap(out: anytype, comptime sequence: []const u8, count: usize) void
const rem = count - idx;
const line_length = @min(@as(usize, max_line_length), rem);

_ = out.write(padded_sequence[off .. off + line_length]) catch unreachable;
_ = out.writeByte('\n') catch unreachable;
_ = out.write(padded_sequence[off .. off + line_length]) catch unreachable;
_ = out.writeByte('\n') catch unreachable;

off += line_length;
if (off > sequence.len) {
Expand Down Expand Up @@ -71,19 +71,22 @@ fn generateAndWrap(out: anytype, comptime nucleotides: []const AminoAcid, count:
}

line[line_length] = '\n';
_ = out.write(line[0 .. line_length + 1]) catch unreachable;
_ = out.write(line[0 .. line_length + 1]) catch unreachable;
idx += line_length;
}
}

var buffer: [256]u8 = undefined;
var fixed_allocator = std.heap.FixedBufferAllocator.init(buffer[0..]);
var allocator = &fixed_allocator.allocator;
const Out = struct {
pub fn write(_: Out, bytes: []const u8) !usize {
return try std.posix.write(std.posix.STDOUT_FILENO, bytes);
}
pub fn writeByte(self: Out, b: u8) !void {
_ = try self.write(&[_]u8{b});
}
};

pub fn main() !void {
var buffered_stdout = std.io.bufferedWriter(std.io.getStdOut().writer());
defer buffered_stdout.flush() catch unreachable;
const stdout = buffered_stdout.writer();
const stdout = Out{};
const n = try get_n();
const homo_sapiens_alu = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTC" ++
"AGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCG" ++
Expand Down
7 changes: 4 additions & 3 deletions bench/algorithm/helloworld/1.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const std = @import("std");
const print = @import("print.zig");
const global_allocator = std.heap.c_allocator;

pub fn main() !void {
const args = try std.process.argsAlloc(global_allocator);
defer std.process.argsFree(global_allocator, args);
const stdout = std.io.getStdOut().writer();
if (args.len > 1) {
try stdout.print("Hello world {s}!\n", .{args[1]});
try print.printFmt("Hello world {s}!\n", .{args[1]});
} else {
try stdout.print("Hello world!\n", .{});
try print.printFmt("Hello world!\n", .{});
}
}
27 changes: 14 additions & 13 deletions bench/algorithm/json-serde/1.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const std = @import("std");
const print = @import("print.zig");
const json = std.json;

const global_allocator = std.heap.c_allocator;


pub fn main() !void {
const args = try std.process.argsAlloc(global_allocator);
defer std.process.argsFree(global_allocator, args);
Expand All @@ -17,39 +19,38 @@ pub fn main() !void {
if (args.len > 2) {
n = try std.fmt.parseInt(usize, args[2], 10);
}
const stdout = std.io.getStdOut().writer();

const json_str = try file.readToEndAlloc(global_allocator, std.math.maxInt(u32));
defer global_allocator.free(json_str);
{
const parsed = try json.parseFromSlice(GeoData, global_allocator, json_str, .{});
defer parsed.deinit();
const data = parsed.value;
var json_str_des = std.ArrayList(u8).init(global_allocator);
defer json_str_des.deinit();
try json.stringify(data, .{}, json_str_des.writer());
try printHash(json_str_des.items, stdout);
var json_str_des: std.ArrayList(u8) = .{};
defer json_str_des.deinit(global_allocator);
try json.stringify(data, .{}, json_str_des.writer(global_allocator));
try printHash(json_str_des.items);
}
{
var array = std.ArrayList(GeoData).init(global_allocator);
var array: std.ArrayList(GeoData) = .{};
var i: usize = 0;
while (i < n) : (i += 1) {
const parsed = try json.parseFromSlice(GeoData, global_allocator, json_str, .{});
// defer parsed.deinit();
try array.append(parsed.value);
try array.append(global_allocator, parsed.value);
}
var json_str_des = std.ArrayList(u8).init(global_allocator);
defer json_str_des.deinit();
try json.stringify(array.items, .{}, json_str_des.writer());
try printHash(json_str_des.items, stdout);
var json_str_des: std.ArrayList(u8) = .{};
defer json_str_des.deinit(global_allocator);
try json.stringify(array.items, .{}, json_str_des.writer(global_allocator));
try printHash(json_str_des.items);
}
}

fn printHash(bytes: []const u8, stdout: anytype) !void {
fn printHash(bytes: []const u8) !void {
const Md5 = std.crypto.hash.Md5;
var hash: [Md5.digest_length]u8 = undefined;
Md5.hash(bytes, &hash, .{});
try stdout.print("{s}\n", .{std.fmt.fmtSliceHexLower(&hash)});
try print.printFmt("{s}\n", .{std.fmt.fmtSliceHexLower(&hash)});
}

const GeoData = struct {
Expand Down
Loading
Loading