社区
C语言
帖子详情
急!!!如何在c语言中获取程序运行时间?精度到毫秒,谢谢
luodike
2006-01-09 10:42:14
如何在c语言中获取程序运行时间?精度到毫秒,谢谢,新手,详细点
...全文
1967
10
打赏
收藏
急!!!如何在c语言中获取程序运行时间?精度到毫秒,谢谢
如何在c语言中获取程序运行时间?精度到毫秒,谢谢,新手,详细点
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
柯本
2006-01-10
打赏
举报
回复
如果你的windows下,可简介地用(ms级):
unsigned long t;
t=GetTickCount();
//你的程序
printf("%ld\n",GetTickCount()-t); //得到ms
cqpp
2006-01-10
打赏
举报
回复
#include <time.h>
#include <sys/timeb.h>
struct timeb tp;
struct tm *tt;
ftime( &tp );
tt = localtime( &tp );
hour = tt->tm_hour;
min = tt->tm_min;
sec = tt->tm_sec;
thousand = tp.millitm;
屋顶上的老猫
2006-01-10
打赏
举报
回复
程序笔误 start-end 改 end-start
屋顶上的老猫
2006-01-10
打赏
举报
回复
使用clock() 所在文件time.h
例如:
clock_t start,end;
start=clock();
{
运行的程序;
}
end=clock();
printf("%d",start-end); //毫秒
printf("%f",(start-end)/CLK_TCK); //秒 CLK_TCK为IRQ0的中断值18.2
注意一点,IRQ0的中断时间为55ms,也就是最小时间单位为55ms!
goodluckyxl
2006-01-10
打赏
举报
回复
http://community.csdn.net/Expert/topic/2875/2875958.xml?temp=.3457758
ox_thedarkness
2006-01-09
打赏
举报
回复
这里有个更精确的,精确到时钟周期,可惜不容易转换成秒:
代码:
=======================================================
class PerCounter{
unsigned int _dwLow, _dwHigh;
public:
void startCount(){
_asm{
rdtsc
mov ebx, this
mov [ebx]._dwLow, eax
mov [ebx]._dwHigh, edx
}
};
void stopCount(){
_asm{
rdtsc
mov ebx, this
sub eax, [ebx]._dwLow
sub edx, [ebx]._dwHigh
mov [ebx]._dwLow, eax
mov [ebx]._dwHigh, edx
}
};
friend ostream& operator<< ( ostream& out, PerCounter& pc ){
return out<<pc._dwHigh<<':'<<pc._dwLow;
};
};
=======================================================
用法:
=======================================================
PerCounter myCounter;
//.....
myCounter.startCount();
Test();
myCounter.stopCount();
cout<<myCounter<<endl;
=======================================================
cn_archer
2006-01-09
打赏
举报
回复
有人贴代码了,那我就贴一点原理知识吧。
注:以下内容由网上收集,非本人原创……
如果是MFC可以用CTime类,如果不是就用如下介绍的方法了
很多情況下我們必須取得系統時間(像是交作業...),
但是系統時間要如何取得呢?
TurboC 提供了 time(); (被定義在 <time.h> 中) 這個函式供我們取得系統時間,
當我們執行了 time(NULL); 時, 該函式便會傳回一個 time_t 型態(被定義在 <time.h> 中)
的數字, 單位是"秒", 所以我們應該先用 time_t 宣告一個變數, 來接收 time() 的傳回值:
time_t T; // 宣告變數 T 為 time_t 型態 //
T = time(NULL); // 變數 T 等於 time(NULL) 的傳回值 //
於是變數 T 便接收到了一個單位為"秒"的超大整數, 而這個整數所代表的意義就是:
從西元 1970年 1月 1日 0點 0分 0秒 到目前所經過的秒數,
哇! 這不就是我們所要的系統時間嗎....
但是如果不能換成我們所熟悉的<年/月/日 時:分:秒>表示法就沒意義了.
<如何將系統時間轉成一般表示法>
好不容易取得的系統時間居然長這副德性, 數學好的同學可能會想要排除萬難,
利用精湛的除法, 解決閏年的問題, 一個個將年/月/日/時/分/秒算出來...
不過這樣一來考試題目可能得帶回家寫,
TurboC 又提供了一個 localtime(); (被定義在 <time.h> 中) 這個函式就有分割
time_t 型態資料的功能, 根據 on-line Help 的介紹大致如下:
struct tm * localtime( time_t * );
----------- --------- --------
從這裡可以看出 localtime 需要知道一個 time_t 型態的資料的位址,
然後傳回一個 struct tm 結構的資料的位址.
這下問題來了, 要告訴 localtime() time_t 變數的位址很簡單, 方法如下:
time_t T; // 宣告一個 time_t 型態的變數 T //
T = time(NULL); // T = 系統時間 //
localtime(&T); // 將 T 的位址 &T 丟給 localtime() //
這樣就好了, 但是 struct tm 到底是什麼呢?
(struct 就是結構, 用法類似 class 但無 Member function )
在 on-line Help 中看到 tm 的介紹如下:
<TIME.H> // 被 <time.h> 定義 //
struct tm {
int tm_sec; /* 秒 (0--59) */
int tm_min; /* 分 (0--59) */
int tm_hour; /* 時 (0--23) */
int tm_mday; /* 日 of month (1--31) */
int tm_mon; /* 月 (0--11 記得自己 + 1) */
int tm_year; /* 年 (要把這個值 +1900 才是西元年紀喔) */
int tm_wday; /* 日 of Week (0--6; Sunday = 0) */
int tm_yday; /* 日 of year (0--365) */
int tm_isdst; /* 0 if daylight savings time is not in effect) */
};
看到這個就大概可以知道, localtime() 先將我們給的 time_t 資料換算成
年/月/日/時/分/秒, 然後依照結構 tm 的順序放在記憶體中,
並將該記憶體位址傳回.
原來如此, 那我們不就只要宣告一個 tm 型態的指標, 然後將該指標指向
localtime() 的傳回值, 然後再讀取該指標的內容就好啦^^ 馬上試試:
#i nclude<time.h>
#i nclude<iostream.h>
void main(void)
{
int year, mon, day; // 要用來存日期的 //
int hour, min, sec; // 要用來存時間的 //
time_t T; // 要用來存系統時間的 //
struct tm *TimeP; // 要用來指到 localtime() 傳回的位址的 //
T = time(NULL); // 將系統時間存到 T //
TimeP = localtime( &T ); // TimeP 指到 localtime 算好的資料所在位址 //
// 接下來就來看 TimeP 所指到的地方放了什麼 ^^ //
year = ( TimeP->tm_year ) + 1900;
mon = ( TimeP->tm_mon ) + 1;
day = ( TimeP->tm_mday );
hour = ( TimeP->tm_hour );
min = ( TimeP->tm_min );
sec = ( TimeP->tm_sec );
cout << year << "/" << mon << "/" << day << endl;
cout << hour << ":" << min << ":" << sec << endl;
}
結果果然 localtime() 已經幫我們把 T 的資料換算好了(感動~~),
而我們從 TimeP 指出去的各成員也都傳回正確的數據, 連日期都沒問題^^
總之, 我們已經學會如何取得系統時間了,
剩下的就看各位要如何運用了.
Rick_ang
2006-01-09
打赏
举报
回复
#include <iostream>
int main()
{
clock_t tBegin,tEnd,Time;
tBegin=clock();
/*
Your function here
*/
tEnd=clock();
Time=tEnd-tBegin;
cout<<"Time: "<<(double)Time<<endl;
return 0;
}
ma100
2006-01-09
打赏
举报
回复
错了:更正:
printf ( "%d" ,
( t2.ti_hour - t1.ti_hour ) *60 * 60 * 1000 +
( t2.ti_min - t1.ti_min ) *60 *1000 +
( t2.ti_sec - ti.ti_sec ) * 1000
( t2.ti_hund - ti.ti_hund ) /10 );
ma100
2006-01-09
打赏
举报
回复
#include <stdio.h>
#include <dos.h>
int main(void)
{
struct time t1,t2;
gettime(&t1);
...
gettime(&t2);
printf ( "%d" ,
( t2.ti_hour - t1.ti_hour ) *60 * 60 * 1000 +
( t2.ti_min - t1.ti_min ) *60 *1000 +
( t2.ti_sec - ti.ti_sec ) /10 );
return 0;
}
C语言
计算
程序运行时间
(精确到
毫秒
/微秒)
C语言
要计算程序的运行时间,这里总结两种方法,一种是精确到
毫秒
时间,一种是精确到微秒时间 方法一:精确到微秒时间 #include #include int main() { int a[10002]; int i = 0; double run_time; _LARGE_...
c语言
中
得出
程序运行时间
,
C语言
计算
程序运行时间
?
程序代码: 运行结果: ...这个函数返回从“开启这个程序进程”到“程序
中
调用clock()函数”时之间的CPU时钟计时单元(clocktick)数,在MSDN
中
称之为挂钟时间(wal-clock)。其
中
clock_t是用来保存时间的数据类型,在ti...
ctimespan
获取
毫秒
_
C语言
获取
系统时间的几种形式
2013-10-10浏览:121次
C语言
获取
系统时间的几种方式
C语言
获取
系统时间的几种方式2012年01月10日
C语言
获取
系统时间的几种方式
C语言
中
如何
获取
时间?
精度
如何?1 使用time_t time( time_t * timer ) 精确到秒...
C语言
如何获得精确到
毫秒
的时间
time命令来计算程序运行运行所消耗的时间,能精确到
毫秒
,如果要精确到代码块或某个操作运行时所消耗的时间,time命令就不给力了。如果对时间的
精度
要求不高的话,可以调用标准C的接口time来得到开始和结束的时间,...
c语言
计算时间差 微秒_
C语言
计算
程序运行时间
(精确到
毫秒
/微秒)
C语言
计算程序的运行时间,这里总结两种方法,一种是精确到
毫秒
时间,一种是精确到微秒时间windows方法一:精确到微秒时间函数#include#include int main() {int a[10002];int i = 0;double run_time;_LARGE_...
C语言
69,371
社区成员
243,082
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章