13

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;
}
Kaer
  • 351
  • 2
  • 5
  • 14

6 Answers6

37

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.

Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • According to http://man7.org/linux/man-pages/man3/ftime.3.html, ftime() is obsolete. Perhaps a better cross-platform solution would be to use the [`std::chrono`](http://en.cppreference.com/w/cpp/chrono) library. – erobertc Mar 14 '16 at 22:16
  • 9
    @erobertc The OP needs a solution for `C` and not `C++`. Your suggestion requires `C++`. – rbaleksandar Jan 14 '17 at 13:55
13

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

mcrisc
  • 809
  • 1
  • 9
  • 19
  • you can't use `timediff` in this line: `elapsed = timediff(t1, t2);` unless define the correct header file. Just `t2-t1` will work fine. – inckka Jul 26 '16 at 08:57
  • 1
    @inckka, `timediff()` is defined right above `main()`. No header file is needed. And `t2-t1` won't give you the answer in milliseconds. – mcrisc Jul 26 '16 at 14:51
  • you are correct. Sorry for the careless misunderstanding. – inckka Jul 26 '16 at 14:53
  • 3
    you should be careful in using `clock()` on a multithreaded program, as it returns "CPU time consumed by the program" rather than the time elapsed – Ameer Jewdaki Aug 03 '17 at 09:40
2

For Windows, GetSystemTime() is what you want. For POSIX, gettimeofday().

Imre Kerr
  • 2,388
  • 14
  • 34
2

This code piece works. This is based on the answer from Angus Comber:

#include <sys/timeb.h>

uint64_t system_current_time_millis()
{
#if defined(_WIN32) || defined(_WIN64)
    struct _timeb timebuffer;
    _ftime(&timebuffer);
    return (uint64_t)(((timebuffer.time * 1000) + timebuffer.millitm));
#else
    struct timeb timebuffer;
    ftime(&timebuffer);
    return (uint64_t)(((timebuffer.time * 1000) + timebuffer.millitm));
#endif
}
techcraver
  • 401
  • 3
  • 21
1

GetSystemTime() uses the structure SYSTEMTIME, which provides milli-second resolution.

More on this here.

alk
  • 69,737
  • 10
  • 105
  • 255
0
DWORD start = GetTickCount();
executeSmth();
printf("Elapsed: %i ms", GetTickCount() - start);

P.S. This method has some limitations. See GetTickCount.

Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31