Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current time in milliseconds in C Programming [duplicate]

Tags:

c

time.h

Possible Duplicate:
How to measure time in milliseconds using ANSI C?
How can I get the Windows system time with millisecond resolution?

We want to calculate the time which a player have taken to finish the game. But with time.h we could only calculate in seconds. but that is not exact. Is it possible to get the time in milliseconds? and what is the %? to printf?

like image 806
kk-dev11 Avatar asked Dec 19 '11 08:12

kk-dev11


2 Answers

There is no portable way to get resolution of less than a second in standard C So best you can do is, use the POSIX function gettimeofday().

like image 95
Alok Save Avatar answered Oct 05 '22 12:10

Alok Save


quick answer

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

int main()   
{   
    clock_t t1, t2;  
    t1 = clock();   
    int i;
    for(i = 0; i < 1000000; i++)   
    {   
        int x = 90;  
    }   

    t2 = clock();   

    float diff = ((float)(t2 - t1) / 1000000.0F ) * 1000;   
    printf("%f",diff);   

    return 0;   
}
like image 29
Mustafa Ekici Avatar answered Oct 05 '22 13:10

Mustafa Ekici