C++ , 定时器, 毫秒 [英] C++ , Timer, Milliseconds

查看:33
本文介绍了C++ , 定时器, 毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <conio.h>
#include <ctime>



using namespace std;

double diffclock(clock_t clock1,clock_t clock2)
{
    double diffticks=clock1-clock2;
    double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
    return diffms;
}
int main()
{
    clock_t start = clock();
    for(int i=0;;i++)
    {

    if(i==10000)break;
    }
    clock_t end = clock();

    cout << diffclock(start,end)<<endl;

    getch();
return 0;
}

所以我的问题是它返回给我一个 0,准确地说,我想检查我的程序运行了多少时间......我在互联网上发现了大量的废话,大部分都是因为开始和结束是相同的,所以得到 0 是相同的

So my problems comes to that it returns me a 0, well to be stright i want to check how much time my program does operate... I found tons of crap over the internet well mostly it comes to the same point of getting a 0 beacuse the start and the end is the same

这个问题归于 C++ 记住:<

This problems goes to C++ remeber : <

推荐答案

乍一看,您似乎是从较小的值中减去较大的值.你打电话:

At a glance, it seems like you are subtracting the larger value from the smaller value. You call:

diffclock( start, end );

但随后 diffclock 定义为:

But then diffclock is defined as:

    double diffclock( clock_t clock1, clock_t clock2 ) {

        double diffticks = clock1 - clock2;
        double diffms    = diffticks / ( CLOCKS_PER_SEC / 1000 );

        return diffms;
    }

除此之外,它可能与您转换单位的方式有关.本页使用1000转换为毫秒的用法不同:

Apart from that, it may have something to do with the way you are converting units. The use of 1000 to convert to milliseconds is different on this page:

http://en.cppreference.com/w/cpp/chrono/c/时钟

这篇关于C++ , 定时器, 毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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