Skip to content

Commit e69209d

Browse files
committed
Increase time precision of benchmark
1 parent b3100f1 commit e69209d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/livecaptions-welcome.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
#include <april_api.h>
2626
#include <stdio.h>
2727
#include <time.h>
28+
#include <sys/time.h>
29+
30+
uint64_t get_time_us(void) {
31+
struct timeval tv;
32+
gettimeofday(&tv, NULL);
33+
return tv.tv_sec * 1000000 + tv.tv_usec;
34+
}
35+
2836

2937
static void benchmark_dummy_handler(G_GNUC_UNUSED void* _userdata,
3038
G_GNUC_UNUSED AprilResultType _result,
@@ -89,7 +97,7 @@ static void *run_benchmark_thread(void *userdata) {
8997
size_t sr = aam_get_sample_rate(model);
9098
g_assert(sr < 48000);
9199

92-
time_t begin = time(NULL);
100+
uint64_t begin = get_time_us();
93101

94102

95103
int idx = 0;
@@ -100,9 +108,10 @@ static void *run_benchmark_thread(void *userdata) {
100108
self->benchmark_progress_v = ((double)sec) / 30.0;
101109

102110

103-
time_t current = time(NULL);
104-
if(difftime(current, begin) > 55.0) {
105-
double speed = ((double)sec) / difftime(current, begin);
111+
uint64_t current = get_time_us();
112+
double time_diff = (current - begin) / 1000.0 / 1000.0;
113+
if(time_diff > 55.0) {
114+
double speed = ((double)sec) / time_diff;
106115
self->benchmark_result_v = speed;
107116

108117
goto end;
@@ -111,9 +120,10 @@ static void *run_benchmark_thread(void *userdata) {
111120
g_idle_add(update_progress, self);
112121
}
113122

114-
time_t end = time(NULL);
123+
uint64_t end = get_time_us();
124+
double time_diff = (end - begin) / 1000.0 / 1000.0;
115125

116-
double speed = 30.0 / difftime(end, begin);
126+
double speed = 30.0 / time_diff;
117127
self->benchmark_result_v = speed;
118128

119129
end:

0 commit comments

Comments
 (0)