用C测量执行时间(在Windows上) [英] Measure execution time in C (on Windows)

查看:122
本文介绍了用C测量执行时间(在Windows上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的函数或方法来衡量的时间比时钟()在Windows功能?我有一个简短的操作,当我尝试时钟()的GetTickCount()它说,它采取0.0秒。我需要一种方法以毫秒为单位或纳秒来衡量它。

Is there a better function or way to measure time than clock() function on Windows? I have a short operation and when I try clock() or gettickcount() it says it took 0.0 seconds. I need a way to measure it by miliseconds or nanoseconds.

推荐答案

您可以使用 QueryPerformanceCounter的 QueryPerformanceFrequency的

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(void)
{
    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    double interval;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    // code to be measured

    QueryPerformanceCounter(&end);
    interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;

    printf("%f\n", interval);

    return 0;
}

这篇关于用C测量执行时间(在Windows上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆