-
yes command in Zig코딩Coding/Zig_❤️lang 2022. 7. 24. 21:38728x90
yes command in Zig
Hi, I came across a [interesting post](https://endler.dev/2017/yes/) about how to optimize unix command `yes`, I rewrite the code in Zig. Is there any way to improve this code?
## First buffered version
```zig
const std = @import("std");
pub fn main() !void {
const argv = std.os.argv;
const output: []const u8 = if (argv.len > 1)
argv.ptr[1][0..std.mem.len(argv.ptr[1])]
else
"y";
const out = std.io.getStdOut();
var f = std.io.bufferedWriter(out.writer());
while ((try f.write(output)) > 0) {
_ = try f.write("\n");
}
}
```
Compile and run
```
zig build-exe yes.zig -Drelease-safe
./yes | pv -r > /dev/null
```
116Mib/s
## Second reuse buffer version
```zig
const std = @import("std");
const BUFFER_CAP = 64 * 1024;
fn fill_up_buffer(buf: *[BUFFER_CAP]u8, output: []const u8) []const u8 {
if (output.len > buf.len / 2) {
return output;
}
std.mem.copy(u8, buf, output);
std.mem.copy(u8, buf[output.len..], "\n");
var buffer_size = output.len + 1;
while (buffer_size < buf.len / 2) {
std.mem.copy(u8, buf[buffer_size..], buf[0..buffer_size]);
buffer_size *= 2;
}
return buf;
}
pub fn main() !void {
const argv = std.os.argv;
const output: []const u8 = if (argv.len > 1)
argv.ptr[1][0..std.mem.len(argv.ptr[1])]
else
"y";
var buffer: [BUFFER_CAP]u8 = undefined;
const body = fill_up_buffer(&buffer, output);
const stdout = std.io.getStdOut();
var writer = stdout.writer();
while ((try writer.write(body)) > 0) {}
}
```const std = @import("std"); pub fn main() !void { const argv = std.os.argv; const output: []const u8 = if (argv.len > 1) argv.ptr[1][0..std.mem.len(argv.ptr[1])] else "y"; const out = std.io.getStdOut(); var f = std.io.bufferedWriter(out.writer()); while ((try f.write(output)) > 0) { _ = try f.write("\n"); } } ``` Compile and run ``` zig build-exe yes.zig -Drelease-safe ./yes | pv -r > /dev/null ``` 116Mib/s ## Second reuse buffer version ```zig const std = @import("std"); const BUFFER_CAP = 64 * 1024; fn fill_up_buffer(buf: *[BUFFER_CAP]u8, output: []const u8) []const u8 { if (output.len > buf.len / 2) { return output; } std.mem.copy(u8, buf, output); std.mem.copy(u8, buf[output.len..], "\n"); var buffer_size = output.len + 1; while (buffer_size < buf.len / 2) { std.mem.copy(u8, buf[buffer_size..], buf[0..buffer_size]); buffer_size *= 2; } return buf; } pub fn main() !void { const argv = std.os.argv; const output: []const u8 = if (argv.len > 1) argv.ptr[1][0..std.mem.len(argv.ptr[1])] else "y"; var buffer: [BUFFER_CAP]u8 = undefined; const body = fill_up_buffer(&buffer, output); const stdout = std.io.getStdOut(); var writer = stdout.writer(); while ((try writer.write(body)) > 0) {} }
반응형'코딩Coding > Zig_❤️lang' 카테고리의 다른 글
How Zig is used at Uber - Motiejus Jakstys (0) 2022.08.05 Zig) ❤️Why no function overloading? (0) 2022.08.02 Zig Support plugin for IntelliJ and CLion version 0.0.7 released (0) 2022.07.29 On Transpilers (Zig/Rust/Go)❤️⭐️ (0) 2022.07.27 Zig translate-c and linking equal best friends (0) 2022.07.19 Zig Language Server (0) 2022.07.18 Zig debug setting (0) 2022.07.18 zig) thoughts on array multiplication and array concat (0) 2022.07.13