Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time elapsed in C in milliseconds? (Windows)

Tags:

c

time

elapsed

I've searched in the Web but I've only found a way for do it, but in this way it returns in seconds instead of milliseconds.

My code is:

#include <stdio.h>
#include <assert.h>
#include <time.h>

int main(void)
{
    int solucion;

    time_t start, stop;
    clock_t ticks;
    long count;

    time(&start);
    solucion = divisores_it(92000000, 2);
    time(&stop);

    printf("Finnished in %f seconds. \n", difftime(stop, start));
    return 0;
}
like image 562
Kaer Avatar asked Jun 22 '13 12:06

Kaer


People also ask

How do you calculate elapsed time in C?

To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.

Which function returns the time difference in milliseconds?

Which function returns the time difference in milliseconds? You can use DiffSeconds() built-in function and multiply the result by 1000. The output will be milliseconds.

What is timestamp in C?

timestamp, a C code which prints the YMDHMS date as a timestamp. This is useful when documenting the run of a program. By including a timestamp, the output of the program will always contain a clear indication of when it was created.

Can Time_t store milliseconds?

time_t almost always represents seconds. Check out clock_t instead. It often has nanosecond precision, you can get the exact amount with the CLOCKS_PER_SEC macro, and you get a clock_t by calling clock() (instead of time() ).


2 Answers

A cross platform way is to use ftime.

Windows specific link here: http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx

Example below.

#include <stdio.h>
#include <sys\timeb.h> 

int main()     
{ 
    struct timeb start, end;
    int diff;
    int i = 0;
    ftime(&start);

    while(i++ < 999) {
        /* do something which takes some time */
        printf(".");    
    }

    ftime(&end);
    diff = (int) (1000.0 * (end.time - start.time)
        + (end.millitm - start.millitm));

    printf("\nOperation took %u milliseconds\n", diff);
    return 0;
}

I ran the code above and traced through it using VS2008 and saw it actually calls the windows GetSystemTimeAsFileTime function.

Anyway, ftime will give you milliseconds precision.

like image 138
Angus Comber Avatar answered Oct 19 '22 17:10

Angus Comber


The solution below seems OK to me. What do you think?

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

long timediff(clock_t t1, clock_t t2) {
    long elapsed;
    elapsed = ((double)t2 - t1) / CLOCKS_PER_SEC * 1000;
    return elapsed;
}

int main(void) {
    clock_t t1, t2;
    int i;
    float x = 2.7182;
    long elapsed;

    t1 = clock();
    for (i=0; i < 1000000; i++) {
           x = x * 3.1415; 
    }
    t2 = clock();

    elapsed = timediff(t1, t2);
    printf("elapsed: %ld ms\n", elapsed);


    return 0;
}

Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html#clock

like image 20
mcrisc Avatar answered Oct 19 '22 17:10

mcrisc