1. notepad++ 運行c語言程序怎麼顯示運行時間
#include<time.h>
intmain()
{
longstart,end;
start=clock();
//測試的程序段
end=clock();
printf("%ld ",start-end);//單位:毫秒
return0;
}
2. 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;
}
3. C語言裡面怎樣測出程序運算的時間
#include <time.h>
#include <stdio.h>
int main()
{
//...
double Times = clock() / (double)(CLOCKS_PER_SEC);
printf("%.2lf\n",Times);
//...
return 0;
}
簡單地說,就是使用time.h庫中的clock()函數。
具體講,程序中的clock()函數在time.h中聲明,每次調用它,返回程序自開始運行至此經歷的時間,單位是(1/CLOCKS_PER_SEC)秒,CLOCKS_PER_SEC是在time.h中聲明的一個常數,在Windows環境下,它的值是1000,也就是說若程序執行了1.5s,調用clock()函數會返回1500;在Linux或Unix環境下,它的值是1000000,即此時clock()函數會返回1500000,為了增強程序的可移植性,故通用寫法為clock() / (double)(CLOCKS_PER_SEC)。
4. 怎樣知道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;
}
5. 如何在c語言中記錄程序運行時間
你要讓C語言編寫的程序像軟體一樣運行,首先你要建好應用程序的工程,然後才能在這個基礎上寫程序
C編寫如你所說的像軟體一樣的程序,需要基於很多底層庫的,所以建立工程很重要,他會幫你加入很多標准庫文件
6. 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");
}
7. 計算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");這個語句的運行時間是可以忽略不計的,加一個次數較多的循環才能看到效果
8. 在c語言中如何取得整個程序的執行時間
#include<stdio.h>
#include<time.h>
int main()
{
clock_t start,end;
start = clock(); //開始時,取得開始時間。
//你自己的代碼
end = clock(); //結束時,取得結束時間
printf("Run time: %lf S",(double)(end-start)/CLOCKS_PER_SEC);
return 0;
}
9. 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;
}