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

c語言計算時間

發布時間: 2023-01-01 17:55:18

㈠ 計算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語言獲取時間

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("當前系統時間:%s",asctime(timeinfo));
return0;
}

說明:
time_t // 時間類型(time.h 定義)
struct tm { // 時間結構,time.h 定義如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
time ( &rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime ( &rawtime ); //轉為當地時間,tm 時間結構
asctime() // 轉為標准ASCII時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1

㈢ 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;}

(3)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語言運行一條語句所用時間如何計算

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語言計算時間函數

標准庫的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語言編程 關於計算時間的問題 望高手解答!

希望能夠我的思路可以幫助你:
①如果password="124567"時,歡迎進入!
②如果password != "124567"時,等待15分鍾!
③等待15分鍾後返回重新輸入密碼!

#include <stdio.h>
#include <string.h>
#include<windows.h>
int main()
{
char str[20], password;
int x,i;
//執行4次循環0,1,2,3
for(x=0; x<=3 && strcmp(str,"1234567")!=0; x++)
{
printf("Enter password please:");
scanf("%s",&str);
//當密碼錯誤時提示輸入錯誤!
if(strcmp(str,"1234567")!=0)
{
printf("Input error!\n");
}
//當錯誤了3次時執行等待,並重置x的初值
if(x==2)
{
printf("Please wait another 15 min.");
for(i=0;i<=(15*60);i++)
Sleep(1000); //停滯一秒
//重置x的初值
x=0;
}
else
//密碼輸入正確時跳出循環,執行for循環之外的語句
{
if(strcmp(str,"1234567")==0)
printf("Welcome\n");
break;
}
}
//可以插入驗證後要執行的代碼
return 0;
}

㈦ 怎樣計算程序的執行時間(C語言中)

在c語言中有專門處理系統時間,程序計時等等功能的庫,
即time.h
在time.h中函數clock_t clock( void )可以完成計時功能。

這個函數返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數,在MSDN中稱之為掛鍾時間(wal-clock)。其中clock_t是用來保存時間的數據類型,在time.h文件中,我們可以找到對它的定義:
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif

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

#define CLOCKS_PER_SEC ((clock_t)1000)

可以看到每過千分之一秒(1毫秒),調用clock()函數返回的值就加1。

下面這個程序計算了循環1千萬次所用的時間:

#include 「stdio.h」
#include 「stdlib.h」
#include 「time.h」

int main( void )
{
long i = 10000000L;
clock_t start, finish;
double ration;
/* 測量一個事件持續的時間*/
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- ) ;
finish = clock();
ration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", ration );
system("pause");
}

運行結果如下:

Time to do 10000000 empty loops is 0.03000 seconds

參考資料:http://www.zxbc.cn/html/cjjhs/0312542045823.html

㈧ 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語言編程,怎麼計算時間

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

}

(9)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語言 計算時間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,我已經重載過+號運算符了,能夠對時間類進行加法運算!

熱點內容
ftp伺服器被動模式配置 發布:2025-07-04 05:17:32 瀏覽:331
電動車小龜有哪些配置 發布:2025-07-04 05:16:18 瀏覽:39
mysql同步存儲過程 發布:2025-07-04 05:14:32 瀏覽:662
安卓手機如何控制空調 發布:2025-07-04 05:09:06 瀏覽:154
新潔爾滅用於物體表面怎麼配置 發布:2025-07-04 05:03:28 瀏覽:829
生活中的雲伺服器 發布:2025-07-04 05:01:55 瀏覽:744
三星g6700c原始密碼是多少 發布:2025-07-04 04:49:41 瀏覽:726
網頁編程代碼 發布:2025-07-04 04:47:25 瀏覽:805
發消息時用到什麼密碼 發布:2025-07-04 04:41:47 瀏覽:980
3個密碼箱能裝多少錢 發布:2025-07-04 04:39:36 瀏覽:11