Skip to content

Commit

Permalink
Merge pull request #2 from byte911/main
Browse files Browse the repository at this point in the history
Add 03-01 elapsed time
  • Loading branch information
jiacai2050 authored Dec 13, 2023
2 parents cd8f512 + 0403852 commit b9c8c4f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ jobs:
- name: Run examples(Unix)
if: matrix.os != 'windows-latest'
working-directory: examples
run: zig build run-all --summary all
run: |
zig fmt --check src/
zig build run-all --summary all
- name: Run examples(Windows)
if: matrix.os == 'windows-latest'
working-directory: examples
run: zig.exe build run-all --summary all
run: |
zig.exe fmt --check src/
zig.exe build run-all --summary all
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
book
zig-out/
zig-cache/
.DS_Store
20 changes: 20 additions & 0 deletions examples/src/03-01.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const std = @import("std");
const time = std.time;
const Instant = time.Instant;
const print = std.debug.print;

fn expensive_function() void {
// sleep 500ms
time.sleep(time.ns_per_ms * 500);
}

pub fn main() !void {
const start = try Instant.now();
expensive_function();
const now = try Instant.now();
const elapsed_ns: f64 = @floatFromInt(now.since(start));

print("Time elapsed in expensive_function() is: {d:.3}ms", .{
elapsed_ns / time.ns_per_s,
});
}
34 changes: 34 additions & 0 deletions src/03-01-elapsed-time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### Measure the elapsed time between two code sections

Measures [`std.time.Instant.since`] since [`std.time.Instant.now`].

Calling [`std.time.Instant.since`] returns a u64 representing nanoseconds elapsed.

```zig
const std = @import("std");
const time = std.time;
const Instant = time.Instant;
const print = std.debug.print;
fn expensive_function() void {
// sleep 500ms
time.sleep(time.ns_per_ms * 500);
}
pub fn main() !void {
const start = try Instant.now();
expensive_function();
const now = try Instant.now();
const elapsed_ns: f64 = @floatFromInt(now.since(start));
print("Time elapsed in expensive_function() is: {d:.3}ms", .{
elapsed_ns / time.ns_per_s,
});
}
```

[`std.time.ns_per_s`]: https://ziglang.org/documentation/master/std/#A;std:time.ns_per_s
[`std.time.Instant.since`]: https://ziglang.org/documentation/master/std/#A;std:time.Instant.since
[`std.time.Instant.now`]: https://ziglang.org/documentation/master/std/#A;std:time.Instant.now
[`std.time.Instant`]:https://ziglang.org/documentation/master/std/#A;std:time.Instant
Measure the elapsed time
3 changes: 3 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

- [Cryptography]()
- [Calculate SHA-256 digest of a file](./02-01-sha-digest.md)

- [Date and Time]()
- [Measure the elapsed time between two code sections](./03-01-elapsed-time.md)

0 comments on commit b9c8c4f

Please sign in to comment.