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

我在网上搜索过但我只找到了一种方法,但是这样它就会以秒为单位而不是几毫秒.

我的代码是:

#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++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...