From 3deea4df0e2ba774f13b011423d2c688b979932d Mon Sep 17 00:00:00 2001 From: fang Date: Mon, 11 Apr 2022 00:43:45 +0200 Subject: [PATCH] u3: improve %bout time printing Now prints micro-, milli-, or full seconds, in the style of u3a_print_memory (|mass). --- pkg/urbit/include/noun/allocate.h | 5 +++++ pkg/urbit/noun/allocate.c | 21 +++++++++++++++++++++ pkg/urbit/noun/nock.c | 6 +++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/urbit/include/noun/allocate.h b/pkg/urbit/include/noun/allocate.h index 7b9d84315..cc30b18fa 100644 --- a/pkg/urbit/include/noun/allocate.h +++ b/pkg/urbit/include/noun/allocate.h @@ -677,6 +677,11 @@ void u3a_lop(c3_w lab_w); + /* u3a_print_time: print microsecond time. + */ + void + u3a_print_time(c3_c* str_c, c3_c* cap_c, c3_d mic_d); + /* u3a_print_memory(): print memory amount. */ void diff --git a/pkg/urbit/noun/allocate.c b/pkg/urbit/noun/allocate.c index 4536069a6..ddee5451b 100644 --- a/pkg/urbit/noun/allocate.c +++ b/pkg/urbit/noun/allocate.c @@ -1922,6 +1922,27 @@ u3a_discount_noun(u3_noun som) } } +/* u3a_print_time: print microsecond time. +*/ +void +u3a_print_time(c3_c* str_c, c3_c* cap_c, c3_d mic_d) +{ + c3_assert( 0 != str_c ); + + c3_w sec_w = (mic_d / 1000000); + c3_w mec_w = (mic_d % 1000000) / 1000; + c3_w mic_w = (mic_d % 1000); + + if ( sec_w ) { + sprintf(str_c, "%s s/%d.%03d.%03d", cap_c, sec_w, mec_w, mic_w); + } + else if ( mec_w ) { + sprintf(str_c, "%s ms/%d.%03d", cap_c, mec_w, mic_w); + } + else { + sprintf(str_c, "%s \xc2\xb5s/%d", cap_c, mic_w); + } +} /* u3a_print_memory: print memory amount. */ diff --git a/pkg/urbit/noun/nock.c b/pkg/urbit/noun/nock.c index 06b7e299d..1140d601e 100644 --- a/pkg/urbit/noun/nock.c +++ b/pkg/urbit/noun/nock.c @@ -1736,8 +1736,8 @@ _n_hilt_hind(u3_noun tok, u3_noun pro) u3_noun p_tok, q_tok; if ( (c3y == u3r_cell(tok, &p_tok, &q_tok)) && (c3__bout == p_tok) ) { u3_atom delta = u3ka_sub(u3i_chub(u3t_trace_time()), u3k(q_tok)); - c3_c str_c[64]; - snprintf(str_c, 63, "took %" PRIu64 "\xc2\xb5s", u3r_chub(0, delta) ); + c3_c str_c[64]; + u3a_print_time(str_c, "took", u3r_chub(0, delta)); u3t_slog(u3nc(0, u3i_string(str_c))); u3z(delta); } @@ -1820,7 +1820,7 @@ _n_hint_hind(u3_noun tok, u3_noun pro) // format the timing report c3_c str_c[64]; - snprintf(str_c, 63, "took %" PRIu64 "\xc2\xb5s", u3r_chub(0, delta) ); + u3a_print_time(str_c, "took", u3r_chub(0, delta)); // join the timing report with the original tank from q_q_tok like so: // "q_q_tok: report"