2

The function displayTimeDifference is not working properly; the issue is that the printf statement is failing. After Googling the format of the printf statement when using a timeval is correct. Not sure why I can't print out the value of the timeval. I'm not getting any system errors from gettimeofday().

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

struct timeval *timeBefore;
struct timeval *timeAfter;
char * Buffer;

double malloctest(const int, const int, const int);
double calloctest(const int, const int, const int);
double allocatest(const int, const int, const int);
void   displayTimeDifference();

int main(int argc, char **argv)
{
    malloctest(3072, 10, 10);
    return 0;
}

double malloctest(const int objectsize, const int numobjects, const int numtests)
{
    int i;
    int retVal;
    for (i = 1; i < numtests; i++) {
        if ((retVal = gettimeofday(timeBefore, NULL)) != 0) {
            printf("ERROR: gettimeofday failed with code: %d\n", retVal);
        }

        Buffer = (char*)malloc(objectsize * sizeof(char));

        if ((retVal = gettimeofday(timeAfter, NULL)) != 0) {
            printf("ERROR: gettimeofday failed with code: %d\n", retVal);
        }

        displayTimeDifference();
    }

    return 0.0;
}



void displayTimeDifference()
{
    printf("Time in microseconds: %ld microseconds\n", (timeAfter->tv_sec - timeBefore->tv_sec));
}
Haris
  • 12,120
  • 6
  • 43
  • 70
Angel
  • 311
  • 1
  • 4
  • 16
  • where is it failing? did you debug it? what's with `Buffer`? What is the *exact* error message? – t0mm13b Oct 24 '15 at 14:48
  • Note that although you claim to print microseconds, you attempt to calculate the difference between the two times in seconds. You have to do quite a bit more work in case you get time before of 123.987654 and a time after of 125.012987. – Jonathan Leffler Oct 24 '15 at 15:14

2 Answers2

3

gettimeofday needs a valid pointer to struct timeval, where it can save the informations, you call it with a NULL pointer.

you should change

struct timeval *timeBefore;
struct timeval *timeAfter;

to

struct timeval timeBefore;
struct timeval timeAfter;

and the calls to gettimeofday(&timeBefore, NULL) and gettimeofday(&timeAfter, NULL). You check the return value of this function and print something, but your program continues as it was successfully.

Also
printf("Time in microseconds: %ld microseconds\n", (timeAfter->tv_sec - timeBefore->tv_sec));
to
printf("Time in seconds: %ld microseconds\n", (timeAfter.tv_sec - timeBefore.tv_sec));.
You are only calculating the seconds, not the microseconds.

Another possibility is to malloc the memory for the pointer, but that is not really necessary.

mch
  • 9,424
  • 2
  • 28
  • 42
1

As already said in another answer you have wrongly declared the struct timeval as pointers. I share my timing macros:

#define START_TIMER(begin)  gettimeofday(&begin, NULL) // ;

#define END_TIMER(end)      gettimeofday(&end,   NULL) // ;

//get the total number of sec:
#define ELAPSED_TIME(elapsed, begin, end) \
    elapsed = (end.tv_sec - begin.tv_sec) \
    + ((end.tv_usec - begin.tv_usec)/1000000.0) // ;

Where you have to define the variables:

struct timeval begin, end;
double elapsed;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
terence hill
  • 3,354
  • 18
  • 31
  • 1
    'Tis curious that you force an assignment inside what could otherwise be a useful expression. Because you use `double`, you avoid problems which you run into with the difference if you maintain the time in seconds separately from the time in microseconds (as in 125.012987 - 123.987345). If you separate the seconds and microseconds, you have to deal with negative microseconds and compensating additions and subtractions. – Jonathan Leffler Oct 24 '15 at 15:19
  • Also, get rid of the semicolons after the `#define` lines. They are wrong. If a user wrote: `if (onevar == twovar) ELAPSED_TIME(t_delta, t1, t2); else ELAPSED_TIME(t_delta, t3, t4);`, the code won't compile. Similarly with the start timer and end timer macros. – Jonathan Leffler Oct 24 '15 at 15:21
  • Amongst others, see [Strange errors using timeval struct and gettimeofday — because of semicolons in `#define`](https://stackoverflow.com/questions/10177547). – Jonathan Leffler Oct 24 '15 at 15:34