Skip to content

Commit db904f5

Browse files
committed
timing: replace deprecated gettimeofday() with clock()
gettimeofday() got marked obsolete in POSIX.1-2008 and removed in POSIX.1-2024. Alternatives: * clock() introduced in C89, part of POSIX.1-1995 (and meant for CPU time) * timespec_get() introduced in C11 and part of POSIX.1-2024 * clock_gettime() introduced in POSIX.1-1995, absent in ISO C so requires POSIX feature macros
1 parent 32b597b commit db904f5

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

library/timing.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct _hr_time {
3838
#include <time.h>
3939
#include <sys/time.h>
4040
struct _hr_time {
41-
struct timeval start;
41+
clock_t start;
4242
};
4343
#endif /* _WIN32 && !EFIX64 && !EFI32 */
4444

@@ -88,15 +88,11 @@ unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int r
8888
struct _hr_time *t = (struct _hr_time *) val;
8989

9090
if (reset) {
91-
gettimeofday(&t->start, NULL);
91+
t->start = clock();
9292
return 0;
9393
} else {
94-
unsigned long delta;
95-
struct timeval now;
96-
gettimeofday(&now, NULL);
97-
delta = (now.tv_sec - t->start.tv_sec) * 1000ul
98-
+ (now.tv_usec - t->start.tv_usec) / 1000;
99-
return delta;
94+
clock_t now = clock();
95+
return (now - t->start) / (CLOCKS_PER_SEC * 1000ul);
10096
}
10197
}
10298

0 commit comments

Comments
 (0)