Sau khi đọc this article khoảng thời gian trôi qua, tôi đã viết một mã đơn giản để tính toán thời gian thực hiện một vòng lặp:Làm thế nào để sử dụng struct timeval để có được thời gian thực hiện?
#include <stdio.h>
#include <sys/time.h>
int main (int argc, char** argv) {
struct timeval, tvalBefore, tvalAfter;
gettimeofday (&tvalBefore, NULL);
int i =0;
while (i < 1000) {
i ++;
}
gettimeofday (&tvalAfter, NULL);
printf("Time in microseconds: %0.3f microseconds\n",
(float)(tvalAfter.tv_sec - tvalBefore.tv_sec)
)
return 0;
}
Trình biên dịch kêu vang mang lại cho tôi những lỗi sau đây:
print_time.c:7:16: error: expected identifier or '('
struct timeval, *tvalBefore, *tvalAfter;
^
print_time.c:13:17: error: use of undeclared identifier 'tvalBefore'
gettimeofday (&tvalBefore, NULL);
^
print_time.c:19:17: error: use of undeclared identifier 'tvalAfter'
gettimeofday (&tvalAfter, NULL);
^
print_time.c:22:12: error: use of undeclared identifier 'tvalAfter'
(float)(tvalAfter.tv_sec - tvalBefore.tv_sec)
^
print_time.c:22:31: error: use of undeclared identifier 'tvalBefore'
(float)(tvalAfter.tv_sec - tvalBefore.tv_sec)
^
5 errors generated.
tôi không thể tìm ra những gì sai với mã của tôi, bất kỳ ý tưởng?
Đi ra rằng dấu phẩy sau 'struct timeval' – LSerni
Không sử dụng gettimeofday để đo thời gian thực hiện! Đọc này: http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time và điều này: http://stackoverflow.com/questions/12392278/getrusage -vs-clock-gettime-vs-clock-vs-gettimeofday/12480485 # 12480485 –
@ DouglasB.Staple cảm ơn vì đã cho tôi biết sự cố này – mko