编写程序

[复制链接]
 楼主| wuhany 发表于 2016-9-25 17:35 | 显示全部楼层 |阅读模式
怎样编写一个程序,判断在系统中clock函数衡量CPU时间用的是CPU使用时间还是总流逝时间?
 楼主| wuhany 发表于 2016-9-25 17:40 | 显示全部楼层
不知我描述的对不对,大家理解了没
zhanghqi 发表于 2016-9-25 17:42 | 显示全部楼层
#include <windows.h>
#include <stdio.h>
#include <time.h>

typedef long ClockT;

ClockT start;
ClockT finish;
double dtime;

int main ()
{
     //开始计时保存到start
     start=clock();

    //具体运算
     Sleep(1900);

    //结束计时保存到finish
     finish=clock();

    dtime=(double)(finish-start);
     printf("精确时间: %lf\n",dtime);
     printf("一般时间: %lf\n",dtime/CLOCKS_PER_SEC);
     return 0;
}
zhenykun 发表于 2016-9-25 19:07 | 显示全部楼层
#include <ctime>
#include <iostream>
using namespace std;

int main () {
clock_t start,  finish;   //typedef long clock_t;
double totaltime;   
start = clock();   //clock():Current time of CPU   

for (int i = 0; i < 1000000; i++)   
{
}

finish=clock();   
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;   
cout<<"\nRuntime is: " << totaltime << "s" << endl;   
return 0;
}

中间位置是你需要测试的函数,为简便我只写了一个for循环
 楼主| wuhany 发表于 2016-9-25 19:08 | 显示全部楼层
唉,还是没有什么结果,算了,先结贴吧,多谢大家啦
您需要登录后才可以回帖 登录 | 注册

本版积分规则

879

主题

10435

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部