當前位置:首頁 » 編程語言 » c語言顯示運行時間
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言顯示運行時間

發布時間: 2022-08-11 17:44:29

A. c語言中,我想顯示程序運行時間,請問問題在哪

修改如下,可以精確到毫秒的
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
int main()
{
time_t start,end;start=clock();//用clock(),它可以精確到毫秒的
Sleep(30); //調試的時候發現好像要大於10才能輸出時間
end=clock();
printf("程序運行時間為:%lfs\n",double(end-start)/CLOCKS_PER_SEC);
//CLOCKS_PER_SEC在time.h中定義,為1000,為毫秒換算成秒的基
system("pause");
return 0;
}

B. 用C語言如何列印出一段程序運行的時間

1、在程序最開始的地方獲取一下系統當前時間並保存;

2、在程序結束的地方獲取一下系統當前時間並保存;

3、兩者相減即為程序運行時間,將其列印出來便可。

參考代碼如下:

#include<stdio.h>
#include<time.h>
voidmain()
{
inti;
clock_tt1,t2;
t1=clock();
for(i=0;i<1000000000;i++);
t2=clock();
printf("%d ",t2-t1);
}

C. 怎樣知道C語言運行一個程序用了多長時間

在程序開始時調用一次time()結束時調用一次time(),兩次相減就能獲得秒數。
例:
#include
<time.h>
#include
<stdio.h>
int
main()
{
int
iStartTime
=
time(NULL);
int
i
=
1;
int
iEndTime;
while
(i
>
0)//這里應該放你要運行的程序
{
i++;
}
iEndTime
=
time(NULL);
printf("%ds
elapsed.\n",iEndTime-iStartTime);
return
0;
}
但這個方法精度很低,只能精確到秒。要更高的精確度需要調用操作系統的API。如在Windows下,精確到毫秒級:
#include
<windows.h>
#include
<stdio.h>
int
main()
{
unsigned
uStartTime
=
GetTickCount();//該函數只有在Win2000及以上的版本才被支持
int
i
=
1;
unsigned
uEndTime;
while
(i
>
0)
{
i++;
}
uEndTime
=
GetTickCount();
printf("%ums
elapsed.\n",uEndTime-uStartTime);
return
0;
}

D. 如何在c語言中記錄程序運行時間

你要讓C語言編寫的程序像軟體一樣運行,首先你要建好應用程序的工程,然後才能在這個基礎上寫程序
C編寫如你所說的像軟體一樣的程序,需要基於很多底層庫的,所以建立工程很重要,他會幫你加入很多標准庫文件

E. 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");
}

F. C語言顯示運行的時間 求教 在這段程序上加上什麼能顯示出該程序運行的時間呢

樓主你好!
關於你的問題,可以使用#include<time.h>
你可以考慮定義
clock_t start, finish;
double usetime;
start = clock();

\*
中間為你要測試程序的代碼
*\
finish = clock();

usetime= (double)(finish - start) / CLOCKS_PER_SEC;
printf( "程序用時%f 秒\n", usetime);

希望我的回答對你有幫助!

G. C語言中如何輸出顯示程序的運行時間 望賜教!

BOOLQueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);可以返回硬體支持的高精度計數器的頻率。先調用QueryPerformanceFrequency()函數獲得機器內部計時器的時鍾頻率。接著在需要嚴格計時的事件發生前和發生之後分別調用QueryPerformanceCounter(),利用兩次獲得的計數之差和時鍾頻率,就可以計算出事件經歷的精確時間。

#include"stdafx.h"
#include<windows.h>
#include<time.h>
#include"process.h"
#definerandom(x)(rand()%x)
int_tmain(intargc,_TCHAR*argv[])
{
LARGE_INTEGERfre={0};//儲存本機CPU時鍾頻率
LARGE_INTEGERstartCount={0};
LARGE_INTEGERendCount={0};
QueryPerformanceFrequency(&fre);//獲取本機cpu頻率
//開始計時
QueryPerformanceCounter(&startCount);
//運算

for(inti=0;i<10000000;i++)
{
floatfTem1=random(100)*random(1000)*random(10000)*random(100000);
}
//結束計時
QueryPerformanceCounter(&endCount);
//計算時間差
doubledTimeTake=((double)endCount.QuadPart-(double)startCount.QuadPart)/(double)fre.QuadPart;
printf("用時%f ",dTimeTake);
system("pause");
return0;
}

H. C語言顯示系統時間

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

time_t//時間類型(time.h定義)
structtm{//時間結構,time.h定義如下:
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
}
time(&rawtime);//獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime(&rawtime);//轉為當地時間,tm時間結構
asctime()//轉為標准ASCII時間格式:
//就是直接列印tm,tm_year從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1

I. notepad++ 運行C語言程序怎麼顯示運行時間


#include<time.h>
intmain()
{
longstart,end;
start=clock();

//測試的程序段

end=clock();

printf("%ld ",start-end);//單位:毫秒
return0;
}