Time in C

I can’t find any good time function in C to avoid timeout. I’ve tried with struct timeval, but it didn’t work.

So what function do you use ?

1 Like

What do you mean to avoid timeout? You should not be controlling the execution time. If your code is timing out, it means that it is doing something wrong (infinite loop?).

You can test on your computer, just compiling your code and grabbing one (or more) of the test cases. That way you will be able to run GDB or print debugging messages (the latter also possible within the CodinGame environment).

Hope that helps.
Best regards,
L. Alberto

Hello Saelyos,

This is what I use, keep me posted if it helps you or if it doesn’t

It gives microsecond precision here:

static struct timeval tm1; //declare it global

void start(void) {
    gettimeofday(&tm1, NULL);

void stop(void) {
    struct timeval tm2;
    gettimeofday(&tm2, NULL);
    unsigned long long t = 1000000 * (tm2.tv_sec - tm1.tv_sec) 
                         + (tm2.tv_usec - tm1.tv_usec);
    fprintf(stderr,"time = %llu microsec\n", t);

Hey kirbiby,

Thanks a lot for your answer, it works perfectly now :slight_smile:
I don’t really know what I did wrong, I thought I had the same code, so it’s maybe about the declarations… but anyway that’s fine now !

Hello agimenez,

I need to have the execution time as I sometimes want to do as many simulations as I can during the 100ms given. I hadn’t found the right function but kirbiby gave me the good one !