當前位置:首頁 » 編程語言 » c語言如何計算時間

c語言如何計算時間

發布時間: 2024-06-13 18:11:36

c語言 計算程序運行時間

#include <stdio.h>
#include <time.h>

#define N 10000 /*這個數字可以自己改,如果時間太長結果不出來你就改小點*/
void f()
{
int i, j, k;
for ( i = 0; i < N; ++i )
for ( j = 0; j < N; ++j )
for ( k = 0; k < N; ++k )
;
}

int main()
{
clock_t begin, ration;

begin = clock();
f();
ration = clock() - begin;
printf( "函數f()的運行時間大約為:%dms\n", ration*1000/CLOCKS_PER_SEC );
}

❷ 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 );

}

(2)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語言如何獲取本地時間,然後取時、分、秒的值

#include <stdio.h>

#include <time.h>

int main()

{time_t timep;

struct tm *tp;

time(&timep);

int p;

tp = localtime(&timep); //取得系統時間

printf("Today is %d-%d-%d ", (1900 + tp->tm_year), (1 + tp->tm_mon), tp->tm_mday);

printf("Now is %d:%02d:%02d ", tp->tm_hour, tp->tm_min, tp->tm_sec);

p=tp->tm_sec;

printf("p=%d ",p);

return 0;

}

❹ C語言中 如何獲取系統時間

方法一,#include<time.h>

int main()

{

time_t timep;

struct tm *p;

time (&timep);

p=gmtime(&timep);

printf("%d ",p->tm_sec); /*獲取當前秒*/

printf("%d ",p->tm_min); /*獲取當前分*/

printf("%d ",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/

printf("%d ",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/

printf("%d ",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/

printf("%d ",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/

printf("%d ",p->tm_yday); /*從今年1月1日算起至今的天數,范圍為0-365*/

}

方法二.#include<stdio.h>

#include<time.h>

intmain()

{

time_tt

structtm*lt;time(&t);//獲取Unix時間戳。

lt=localtime(&t);//轉為時間結構。

printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,

lt->tm_hour,lt->tm_min,lt->tm_sec);//輸出結果

return0;}

(4)c語言如何計算時間擴展閱讀

1、CTimeSpan類

如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:

CTime t1( 1999, 3, 19, 22, 15, 0 );

CTime t = CTime::GetCurrentTime();

CTimeSpan span=t-t1; //計算當前系統時間與時間t1的間隔

int iDay=span.GetDays(); //獲取這段時間間隔共有多少天

int iHour=span.GetTotalHours(); //獲取總共有多少小時

int iMin=span.GetTotalMinutes();//獲取總共有多少分鍾

int iSec=span.GetTotalSeconds();//獲取總共有多少秒

2、timeb()函數

_timeb定義在SYSTIMEB.H,有四個fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( &timebuffer );

❺ C語言計算時間

在C語言中計算時間,可以使用標准庫中的計時函數——clock()。

函數原型:

clock_tclock(void);

其中clock_t是用來保存時間的數據類型,在time.h文件中,可以找到對它的定義:

#ifndef_CLOCK_T_DEFINED
typedeflongclock_t;
#define_CLOCK_T_DEFINED
#endif


很明顯,clock_t是一個長整形數。在time.h文件中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一秒鍾會有多少個時鍾計時單元,其定義如下:

#defineCLOCKS_PER_SEC((clock_t)1000)

可以看到每過千分之一秒(1毫秒),調用clock()函數返回的值就加1。下面舉個例子,可以使用公式clock()/CLOCKS_PER_SEC來計算一個進程自身的運行時間:

voidelapsed_time()
{
printf("Elapsedtime:%usecs. ",clock()/CLOCKS_PER_SEC);
}

當然,也可以用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語言編寫一個時間換算

剛才給的那個代碼太繁瑣了,又重新調整了一下:
#include <stdio.h>
int main()
{
//世界協調時與北京時間換算
int a;
scanf("%d",&a); /*輸入北京時間*/
if (a >= 800)
{
printf("%d\n",a-800); /*如果輸入的北京時間大於800,直接減去800,就是世界協調時*/
}
else{

int b=a/100; /*換算百位數字*/
int c=a%100; /*換算十位和個位數字*/
printf("%d\n",(24-(8-b))*100+c);
}

return 0;
}

熱點內容
去哪裡找自己的支付密碼 發布:2024-10-25 14:46:18 瀏覽:417
生產文件夾 發布:2024-10-25 14:46:14 瀏覽:705
windows搭建ftp伺服器埠修改 發布:2024-10-25 14:46:11 瀏覽:322
勞拉與馬ftp 發布:2024-10-25 00:21:16 瀏覽:359
奪寶網站源碼 發布:2024-10-25 00:19:02 瀏覽:454
編程文本編輯器 發布:2024-10-25 00:09:28 瀏覽:972
編程徐帥 發布:2024-10-25 00:03:25 瀏覽:307
手機安卓模擬器如何打開文件 發布:2024-10-25 00:02:55 瀏覽:722
pythonday 發布:2024-10-24 23:55:47 瀏覽:425
g編譯c文件 發布:2024-10-24 23:55:03 瀏覽:294