隨着Linux的使用:
#include <time.h>
double theseSecs = 0.0;
double startSecs = 0.0;
double secs;
double CPUsecs = 0.0;
double CPUutilisation = 0.0;
double answer = 0;
clock_t starts;
void start_CPU_time()
{
starts = clock();;
return;
}
void end_CPU_time()
{
CPUsecs = (double)(clock() - starts)/(double)CLOCKS_PER_SEC;
return;
}
struct timespec tp1;
void getSecs()
{
clock_gettime(CLOCK_REALTIME, &tp1);
theseSecs = tp1.tv_sec + tp1.tv_nsec/1e9;
return;
}
void start_time()
{
getSecs();
startSecs = theseSecs;
return;
}
void end_time()
{
getSecs();
secs = theseSecs - startSecs;
return;
}
void calculate()
{
int i, j;
for (i=1, i<1000001; i++)
{
for (i=j, j<1000001; j++)
{
answer = answer * (float)i/1000000.0;
}
}
}
void main()
{
start_time();
start_CPU_time();
calculate();
end_time();
end_CPU_time();
CPUutilisation = CPUsecs/secs/100.0;
printf"/n Answer %8.3f, Elapsed Time %7.4f, CPU Time %7.4f,
CPU Utilisation %8.4f/n, answer, secs, CPUsecs, CPUutilisation);
}
感謝您的答覆,但只是要確定:)是,回答所需要的時鐘速率? 再次感謝您的幫助:) – nick