【问题标题】:How to get the time elapsed in C in milliseconds? (Windows)如何以毫秒为单位获取 C 中经过的时间? (视窗)
【发布时间】:2013-06-19 12:16:01
【问题描述】:

我在网上搜索过,但我只找到了一种方法,但这样它会以秒而不是毫秒返回。

我的代码是:

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

【问题讨论】:

    标签: c time elapsed


    【解决方案1】:
    DWORD start = GetTickCount();
    executeSmth();
    printf("Elapsed: %i ms", GetTickCount() - start);
    

    附:这种方法有一些局限性。见GetTickCount

    【讨论】:

      【解决方案2】:

      这段代码有效。这是基于 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
      }
      

      【讨论】:

        【解决方案3】:

        下面的解决方案对我来说似乎没问题。你怎么看?

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

        参考:http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html#clock

        【讨论】:

        • 你不能在这一行中使用timediffelapsed = timediff(t1, t2);,除非定义了正确的头文件。只需t2-t1 即可。
        • @inckka, timediff() 定义在 main() 的正上方。不需要头文件。而t2-t1 不会在几毫秒内给你答案。
        • 你是对的。很抱歉造成粗心的误会。
        • 在多线程程序上使用clock() 时应该小心,因为它返回的是“程序消耗的CPU 时间”而不是经过的时间
        【解决方案4】:

        一种跨平台的方式是使用 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 会给你毫秒精度。

        【讨论】:

        【解决方案5】:

        对于 Windows,GetSystemTime() 是您想要的。对于 POSIX,gettimeofday()

        【讨论】:

          【解决方案6】:

          GetSystemTime() 使用SYSTEMTIME 结构,提供毫秒级分辨率。

          More on this here.

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-15
            • 1970-01-01
            • 2011-04-30
            • 1970-01-01
            • 2010-11-30
            相关资源
            最近更新 更多