我需要實現extern void start_stopper(void)和extern void end_stopper()。 如何實現這兩個函數,以便它們能夠計算這個程序執行的秒數?如何在此代碼中實現定時器中斷以計算此斐波那契函數的秒數?
#include <stdio.h>
#include <dos.h>
extern void start_stopper(void);
unsigned long int fibo(int n)
{
if (n <= 1)
return 1;
else
return(fibo(n-1) + fibo(n-2));
} /* fibo */
extern double end_stopper();
void main(void)
{
unsigned long int fn;
unsigned int n;
double time_in_secs;
int i1, i2, result;
printf("Enter an integer <= 47)\n");
scanf("%u", &n);
system("time");
start_stopper();
fn = fibo(n);
system("time");
time_in_secs = end_stopper();
printf("Fibo(%u) = %lu\n", n, fn);
printf("Computation took %lf secs\n", time_in_secs);
} /* main */
容易只是存儲時間和計算的差異。 'time_t time(time_t * seconds)'和'double difftime(time_t time1,time_t time2)'from'time.h'將很容易做到這一點。 – twain249
請閱讀[上一個問題](https://stackoverflow.com/questions/5248915/execution-time-of-c-program)。 –
[C程序執行時間]的可能重複(https://stackoverflow.com/questions/5248915/execution-time-of-c-program) – Sneftel