17

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?

Mick
  • 4,987
  • 1
  • 19
  • 23
mko
  • 21,334
  • 49
  • 130
  • 191
  • 3
    Take out that comma after 'struct timeval' – LSerni Oct 04 '12 at 08:09
  • 4
    Don't use gettimeofday to measure execution time! Read this: http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time and this: http://stackoverflow.com/questions/12392278/getrusage-vs-clock-gettime-vs-clock-vs-gettimeofday/12480485#12480485 – Douglas B. Staple Oct 04 '12 at 17:49
  • 1
    @DouglasB.Staple thanks for letting me know this issue – mko Oct 05 '12 at 12:44
  • @DouglasB.Staple Thanks; I owe you one for linking to proper documentation for that function. (Who cares that it's a blog anyway?) Edit: Oh, the SO link is even better! – wizzwizz4 Apr 09 '16 at 18:42

2 Answers2

38

You have two typing errors in your code:

 struct timeval,

should be

 struct timeval

and after the printf() parenthesis you need a semicolon.

Also, depending on the compiler, so simple a cycle might just be optimized out, giving you a time of 0 microseconds whatever you do.

Finally, the time calculation is wrong. You only take into accounts the seconds, ignoring the microseconds. You need to get the difference between seconds, multiply by one million, then add "after" tv_usec and subtract "before" tv_usec. You gain nothing by casting an integer number of seconds to a float.

I'd suggest checking out the man page for struct timeval.

This is the code:

#include <stdio.h>
#include <sys/time.h>

int main (int argc, char** argv) {
    struct timeval tvalBefore, tvalAfter;  // removed comma

    gettimeofday (&tvalBefore, NULL);
    int i =0;
    while ( i < 10000) {
        i ++;
    }

    gettimeofday (&tvalAfter, NULL);

    // Changed format to long int (%ld), changed time calculation

    printf("Time in microseconds: %ld microseconds\n",
            ((tvalAfter.tv_sec - tvalBefore.tv_sec)*1000000L
           +tvalAfter.tv_usec) - tvalBefore.tv_usec
          ); // Added semicolon
    return 0;
}
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • 2
    "... the man page for 'struct timeval'." What man page is that? On my system there is no man page for 'struct' or 'timeval'. – neirbowj Nov 17 '14 at 00:41
  • 2
    You probably have it either under `man 2 gettimeofday` or `man 3p gettimeofday`. Apparently several manpages tend to be moved around in different distributions. – LSerni Nov 17 '14 at 06:29
13

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.

hmjd
  • 120,187
  • 20
  • 207
  • 252