如何以毫秒为单位获得以C为单位的时间? (视窗)

如何以毫秒为单位获得以C为单位的时间? (视窗),第1张

概述我在网上搜索过但我只找到了一种方法,但是这样它就会以秒为单位而不是几毫秒. 我的代码是: #include <stdio.h>#include <assert.h>#include <time.h>int main(void){ int solucion; time_t start, stop; clock_t ticks; long count; 我在网上搜索过但我只找到了一种方法,但是这样它就会以秒为单位而不是几毫秒.

我的代码是:

#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;}
解决方法 跨平台的方式是使用ftime.

windows特定链接:http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx

以下示例.

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

我运行上面的代码并使用VS2008跟踪它,并看到它实际上调用了windows GetSystemTimeAsfileTime函数.

无论如何,ftime会给你几毫秒的精度.

总结

以上是内存溢出为你收集整理的如何以毫秒为单位获得以C为单位的时间? (视窗)全部内容,希望文章能够帮你解决如何以毫秒为单位获得以C为单位的时间? (视窗)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1238652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存