mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-28 13:53:10 +00:00
Dynamically choose units for time
output
This commit is contained in:
parent
5956270015
commit
1ed5e7e478
1 changed files with 51 additions and 7 deletions
|
@ -100,17 +100,61 @@ int builtin_time(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
||||||
|
|
||||||
uint64_t net_sys_micros = fish_sys_micros + child_sys_micros;
|
uint64_t net_sys_micros = fish_sys_micros + child_sys_micros;
|
||||||
uint64_t net_usr_micros = fish_usr_micros + child_usr_micros;
|
uint64_t net_usr_micros = fish_usr_micros + child_usr_micros;
|
||||||
uint64_t wall_micros = micros(wall[1]) - micros(wall[0]);
|
uint64_t net_wall_micros = micros(wall[1]) - micros(wall[0]);
|
||||||
|
|
||||||
|
enum class unit {
|
||||||
|
minutes,
|
||||||
|
seconds,
|
||||||
|
milliseconds,
|
||||||
|
microseconds,
|
||||||
|
};
|
||||||
|
|
||||||
|
auto get_unit = [](uint64_t micros) {
|
||||||
|
if (micros > 900 * 1E6) {
|
||||||
|
return unit::minutes;
|
||||||
|
}
|
||||||
|
else if (micros > 1 * 1E6) {
|
||||||
|
return unit::seconds;
|
||||||
|
} else if (micros > 10E3) {
|
||||||
|
return unit::milliseconds;
|
||||||
|
} else {
|
||||||
|
return unit::microseconds;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto unit_name = [](unit unit) {
|
||||||
|
switch (unit) {
|
||||||
|
case unit::minutes: return "minutes";
|
||||||
|
case unit::seconds: return "seconds";
|
||||||
|
case unit::milliseconds: return "milliseconds";
|
||||||
|
case unit::microseconds: return "microseconds";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto convert = [](uint64_t micros, unit unit) {
|
||||||
|
switch (unit) {
|
||||||
|
case unit::minutes: return micros / 1.0E6 / 60.0;
|
||||||
|
case unit::seconds: return micros / 1.0E6;
|
||||||
|
case unit::milliseconds: return micros / 1.0E3;
|
||||||
|
case unit::microseconds: return micros / 1.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto wall_unit = get_unit(net_wall_micros);
|
||||||
|
auto cpu_unit = get_unit((net_sys_micros + net_usr_micros) / 2);
|
||||||
|
auto wall_time = convert(net_wall_micros, wall_unit);
|
||||||
|
auto usr_time = convert(net_usr_micros, cpu_unit);
|
||||||
|
auto sys_time = convert(net_sys_micros, cpu_unit);
|
||||||
|
|
||||||
streams.out.append_format(
|
streams.out.append_format(
|
||||||
L"\n__________________________________" \
|
L"\n__________________________________" \
|
||||||
L"\nExecution completed in %f seconds" \
|
L"\nExecuted in %6.2F %s" \
|
||||||
L"\nuser time %f seconds" \
|
L"\n usr time %6.2F %s" \
|
||||||
L"\nsystem time %f seconds" \
|
L"\n sys time %6.2F %s" \
|
||||||
L"\n\n",
|
L"\n\n",
|
||||||
wall_micros / 1.0E6,
|
wall_time, unit_name(wall_unit),
|
||||||
net_usr_micros / 1.0E6,
|
usr_time, unit_name(cpu_unit),
|
||||||
net_sys_micros / 1.0E6
|
sys_time, unit_name(cpu_unit)
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue