Dynamically choose units for time output

This commit is contained in:
Mahmoud Al-Qudsi 2019-12-18 19:21:23 -06:00
parent 5956270015
commit 1ed5e7e478

View file

@ -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_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(
L"\n__________________________________" \
L"\nExecution completed in %f seconds" \
L"\nuser time %f seconds" \
L"\nsystem time %f seconds" \
L"\nExecuted in %6.2F %s" \
L"\n usr time %6.2F %s" \
L"\n sys time %6.2F %s" \
L"\n\n",
wall_micros / 1.0E6,
net_usr_micros / 1.0E6,
net_sys_micros / 1.0E6
wall_time, unit_name(wall_unit),
usr_time, unit_name(cpu_unit),
sys_time, unit_name(cpu_unit)
);
}