You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
nanoTimestamp on windows currently uses GetSystemTimeAsFileTime, however there is a more precise windows api GetSystemTimePreciseAsFileTime. I tried searching for a discussion on why GetSystemTimeAsFileTime is used over GetSystemTimePreciseAsFileTime, but couldn't find anything on the topic. The MSDN documentation states that GetSystemTimePreciseAsFileTime has been available since Windows 8, and based on some issue searching it seems that Windows 7 isn't supported (see this).
So based on that I'd recommend that nanoTimestamp use GetSystemTimePreciseAsFileTime instead. I've written an example program (sorry in 0.13.0 not master) to show that GetSystemTimePreciseAsFileTime does give a better precision for nanoTimestamp on windows.
conststd=@import("std");
constbuiltin=@import("builtin");
pubextern"kernel32"fnGetSystemTimePreciseAsFileTime(*std.os.windows.FILETIME) callconv(std.os.windows.WINAPI) void;
pubfnmain() !void {
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
defer_=gpa.deinit();
varallocator=gpa.allocator();
conststart=std.time.nanoTimestamp();
conststartPrec=nanoTimestamp();
constheap=tryallocator.alloc(usize, 10000);
deferallocator.free(heap);
constend=std.time.nanoTimestamp();
constendPrec=nanoTimestamp();
std.debug.print("The heap alloc took {d}ns\n", .{end-start});
std.debug.print("The heap alloc took {d}ns (precise)\n", .{endPrec-startPrec});
}
fnnanoTimestamp() i128 {
if (builtin.os.tag!=.windows) {
returnstd.time.nanoTimestamp();
}
// FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch,// which is 1601-01-01.constepoch_adj=std.time.epoch.windows* (std.time.ns_per_s/100);
varft: std.os.windows.FILETIME=undefined;
GetSystemTimePreciseAsFileTime(&ft);
constft64= (@as(u64, ft.dwHighDateTime) <<32) |ft.dwLowDateTime;
return@as(i128, @as(i64, @bitCast(ft64)) +epoch_adj) *100;
}
output:
The heap alloc took 0ns
The heap alloc took 13600ns (precise)
Expected Behavior
nanoTimestamp on Windows has better precision.
The text was updated successfully, but these errors were encountered:
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
nanoTimestamp
on windows currently uses GetSystemTimeAsFileTime, however there is a more precise windows api GetSystemTimePreciseAsFileTime. I tried searching for a discussion on whyGetSystemTimeAsFileTime
is used overGetSystemTimePreciseAsFileTime
, but couldn't find anything on the topic. The MSDN documentation states thatGetSystemTimePreciseAsFileTime
has been available since Windows 8, and based on some issue searching it seems that Windows 7 isn't supported (see this).So based on that I'd recommend that
nanoTimestamp
useGetSystemTimePreciseAsFileTime
instead. I've written an example program (sorry in 0.13.0 not master) to show thatGetSystemTimePreciseAsFileTime
does give a better precision fornanoTimestamp
on windows.output:
Expected Behavior
nanoTimestamp
on Windows has better precision.The text was updated successfully, but these errors were encountered: