After reading this article about elapsed time, I wrote a simple code to calculate the execution time of a loop:
#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;
}
The clang compiler gives me the following errors:
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.
I can't figure out what's wrong with my code, any idea?
This question is related to
c
Change:
struct timeval, tvalBefore, tvalAfter; /* Looks like an attempt to
delcare a variable with
no name. */
to:
struct timeval tvalBefore, tvalAfter;
It is less likely (IMO) to make this mistake if there is a single declaration per line:
struct timeval tvalBefore;
struct timeval tvalAfter;
It becomes more error prone when declaring pointers to types on a single line:
struct timeval* tvalBefore, tvalAfter;
tvalBefore
is a struct timeval*
but tvalAfter
is a struct timeval
.
Source: Stackoverflow.com