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

蓝咒 提交于 2019-12-21 03:36:24

问题


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?


回答1:


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;   
}



回答2:


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().




回答3:


If you're on a Unix-like system, use gettimeofday and convert the result from microseconds to milliseconds.



来源:https://stackoverflow.com/questions/8558625/how-to-get-the-current-time-in-milliseconds-in-c-programming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!