c語言時間計算
⑴ c語言運行一條語句所用時間如何計算
把time.h
include進來
然後在代碼的前面和後面都加一條代碼,獲得時間
long
start=clock();
long
end=clock();
兩個減一下就是秒數
⑵ C語言求一個程序運行時間
C/C++中的計時函數是clock()。
所以,可以用clock函數來計算的運行一個循環、程序或者處理其它事件到底花了多少時間,具體參考代碼如下:
#include「stdio.h」
#include「stdlib.h」
#include「time.h」
intmain(void)
{
longi=10000000L;
clock_tstart,finish;
doubleration;
/*測量一個事件持續的時間*/
printf("Timetodo%ldemptyloopsis",i);
start=clock();
while(i--);
finish=clock();
ration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%fseconds ",ration);
system("pause");
}
⑶ C語言運行一條語句所用時間如何計算
unsigned long HighStart,LowStart,HighEnd,LowEnd;
__int64 start =0;
__int64 end = 0;
__int64 timer =0;
//獲取代碼運行開始時cpu內部計數器的值
__asm
{
RDTSC
mov HighStart, edx
mov LowStart, eax
}
for(int i= 0; i<100000; i++ )
{
for(int i= 0; i<100000; i++ )
{
}
}
//獲取代碼結束時cpu內部計數器的值,並減去初值
__asm
{
RDTSC
mov HighEnd, edx
mov LowEnd, eax
}
start = (__int64) HighStart<<32;
start |= (__int64) LowStart;
end = (__int64) HighEnd<<32;
end |= (__int64) LowEnd;
timer = end - start;
//輸出代碼段運行的時鍾周期數
//以頻率1.1Gcpu為例,如果換計算機把其中的2.6改乘其它即可,因為相信大家的cpu都應該在1G以上 ^_^
cout<< (double) (timer /2.6/1000000000) << endl;
return 0;
⑷ c語言編程,怎麼計算時間
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = {10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( &t1 );
_t2 = _mkgmtime( &t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分鍾
", diff / 60 );
}
(4)c語言時間計算擴展閱讀:
C語言中有兩個相關的函數用來計算時間差,分別是:
time_t time( time_t *t) 與 clock_t clock(void)
頭文件: time.h
計算的時間單位分別為: s , ms
time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構
返回值:
1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。
2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。
所以我們可以根據具體情況需求,判斷採用哪一個函數。
具體用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock(); //!< 單位為ms
t_start = time(NULL); //!< 單位為s
system("pause");
c_end = clock();
t_end = time(NULL);
//!<difftime(time_t, time_t)返回兩個time_t變數間的時間間隔,即時間差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。
⑸ c語言如何計算兩個時間相差多少
任意輸入兩個24小時制的時間,輸出兩個時間的時間差;
⑹ C語言計算時間函數
標准庫的time.h里有時間函數
time_t time (time_t *timer)
計算從1970年1月1日到當前系統時間,並把結果返回給timer變數,
函數本身返回的也是這個結果.time_t這個類型其實就是一個int.
另有:
double difftime ( time_t timer2, time_t timer1 )
把返回time2和time1所儲存的時間的差.
⑺ 計算C語言程序運行時間(hello world)
#include "time.h"
#include "stdio.h"
main()
{
double start, finish;
start = clock();//取開始時間
printf("Hello, World!\n");
finish = clock();//取結束時間
printf( "%f seconds\n",(finish - start) / CLOCKS_PER_SEC);//以秒為單位顯示之
}
上面的代碼理論上是可以顯示printf("Hello, World!\n");語句的運行時間的,但我猜實際的顯示結果是0,因為printf("Hello, World!\n");這個語句的運行時間是可以忽略不計的,加一個次數較多的循環才能看到效果
⑻ C語言 計算時間A+B
#include<stdio.h>
struct Time
{
int hours,mins,secs;
};
class TIME
{
Time t;
public:
TIME(){t.hours=0;t.mins=0;t.secs=0;};
Time getTime(){return this->t;}
void settime();
void addtime(Time);
void print();
TIME operator+(TIME);
};
main()
{
TIME a,b,c;
a.settime();
//a.print();
b.settime();
//b.print();
c=a+b;
//c.print();
}
void TIME::settime()
{
printf("請輸入小時,分鍾,秒鍾:(格式按照5h 3m 4s來寫)\n");
scanf("%dh%dm%ds",&this->t.hours,&this->t.mins,&this->t.secs);
printf("\n");
}
void TIME::print()
{
printf("%dh %dm %ds \n",this->t.hours,this->t.mins,this->t.secs);
}
TIME TIME::operator+(TIME t)
{
TIME t1;
t1.t.hours=this->t.hours+t.t.hours;
t1.t.mins=this->t.mins+t.t.mins;
t1.t.secs=this->t.secs+t.t.secs;
if (t1.t.secs>=60)
{
t1.t.mins++;
t1.t.secs-=60;
}
if (t1.t.mins>=60)
{
t1.t.hours++;
t1.t.mins-=60;
}
this->print();
printf("和\n");
t.print();
printf("時間之和為:\n");
t1.print();
return t1;
}
輸入格式按照 XXhXXmXXs 輸入就可以了。輸入不合法也沒有關系。只要別輸入的是字母就OK。輸入2個,就可以輸出答案了。如果還要繼續,你只要添加TIME 類 如TIME d,然
d.settime();是輸入時間,時間相加直接用+表示就OK了,比如d+c,d+a,我已經重載過+號運算符了,能夠對時間類進行加法運算!