HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Helper utilities for easier benchmarking in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
helperforutilitiesbenchmarkingeasier

Problem

I have this tiny library for measuring the execution time in milliseconds and returning the duration plus the result:

execres.h

/******************
* File: execres.h *
******************/

#ifndef EXECRES_H
#define EXECRES_H

#include 

typedef struct {
    unsigned long long  millis;
    void*               result;
} ExecResult, *PExecResult;

PExecResult execTime(void* (*code)(void*), void* param);

#endif  /* EXECRES_H */


execres.c

/******************
* File: execres.c *
******************/

#include "execres.h"
#include 

PExecResult execTime(void* (*code)(void*), void* param)
{
    PExecResult result;
    struct timeval tva;
    struct timeval tvb;

    if (!code)
    {
        return NULL;
    }

    result = malloc(sizeof(ExecResult));

    if (!result)
    {
        return NULL;
    }

    gettimeofday(&tva, NULL);
    result->result = code(param);
    gettimeofday(&tvb, NULL);

    result->millis = (unsigned long long)
                     (tvb.tv_sec * 1000 + tvb.tv_usec / 1000 -
                      tva.tv_sec * 1000 - tva.tv_usec / 1000);
    return result;
}


main.c

/***************
* File: main.c *
***************/

#include 
#include 
#include "execres.h"

static uint64_t Fibonacci(uint64_t n)
{
    switch (n)
    {
        case 0:
            return 0;

        case 1:
            return 1;

        default:
            return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
}

int main()
{
    PExecResult result;

    puts("Begin computation.");

    result = execTime((void* (*)(void*)) Fibonacci, (void*) 40);

    printf("Fibonacci took %u milliseconds and returned %llu.\n",
           (unsigned) result->millis,
           (uint64_t) result->result);

    return 0;
}

Solution

Nice bit of code! Here are a few suggestions:

-
Initialize your variables when you declare them. This helps shorten the code a bit and reduce possible bugs.

-
You need documenting comments within your code. Especially since you are considering this as a library.

-
I would remove the NULL check on code in your execTime method, and document that a valid function pointer should be passed in. I would expect the code to break if I tried to time NULL, not NULL to be returned back. The standard library does this same thing with a few of their functions, like strlen().

-
Good job checking the return from malloc().

-
I would use the C11 standard function timespec_get() for your timing code. It's a C standard (important!) function that has nanosecond precision.

-
If you want a better Fibonacci algorithm, here is an answer I wrote up a while ago on it.

-
Good job using puts() in place of printf() when you can.

-
You don't have to return 0 at the end of main(). The C standard knows how frequently this is used, and lets us omit it.


C99 & C11 §5.1.2.2(3)


...reaching the } that terminates the main() function returns a
value of 0.

Context

StackExchange Code Review Q#131325, answer score: 3

Revisions (0)

No revisions yet.