-1

I need to implemet extern void start_stopper(void) and extern void end_stopper(). How implement these two functions so they will count properely the seconds of this program execution?

#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 */
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Yakov
  • 11
  • Easier to just store the times and calculate the difference. `time_t time(time_t * seconds)` and `double difftime(time_t time1, time_t time2)` from `time.h` will do this easily. – twain249 Jun 04 '17 at 20:28
  • Please read [this previous question](https://stackoverflow.com/questions/5248915/execution-time-of-c-program). – Weather Vane Jun 04 '17 at 20:28
  • Possible duplicate of [Execution time of C program](https://stackoverflow.com/questions/5248915/execution-time-of-c-program) – Sneftel Jun 04 '17 at 20:59
  • Thank you, but I have to implement this using interrupts, interrupt 8 to be pricise – Yakov Jun 04 '17 at 21:26
  • @Yakov: implementing this using interrupts is something that was valuable 20 or 30 years ago. It requires non standard code to declare interrupt handlers and set them up, and it is quite difficult to do this without breaking your system. – chqrlie Jun 04 '17 at 22:55

1 Answers1

2

Instead of using the time shell command, you should use the clock() function defined in <time.h>. It is far more precise with much less overhead.

Here is the modified code:

#include <stdio.h>
#include <time.h>

unsigned long int fibo(int n) {
    if (n <= 1)
        return 1;
    else
        return fibo(n - 1) + fibo(n - 2);
}

int main(void) {
    unsigned long fn;
    unsigned int n;
    double time_in_secs;

    printf("Enter an integer <= 47:\n");
    if (scanf("%u", &n) == 1) {
        clock_t start = clock();

        fn = fibo(n); 

        time_in_secs = (double)(clock() - t) / CLOCKS_PER_SEC;

        printf("Fibo(%u) = %lu\n", n, fn);
        printf("Computation took %f secs\n", time_in_secs);
    }
    return 0;
}

If your are programming on an MS/DOS system, and clock() is not available there, you could try and define it this way:

#define CLOCK_PER_SEC  18.2
#define clock() (*(unsigned long __far *)0x0040006C)

The tick interrupt (INT 8) updates a 32-bit tick counter 65536 times per hour. The 32-bit tick counter is accessible at absolute address 0040:006C. If you want more precise timings, you can try and reprogram the interrupt controller to make it fire interrupts faster. You then need to install your own handler to filter these interrupts to avoid messing the system's tick counter.

Here is a useful site for more info about the PC/DOS system and its BIOS data: http://www.fysnet.net/rombios.htm

Also note that your Fibonacci sequence is not consistent with the classic definition fib(0) = 0, fib(1) = 1. The Wikipedia article is here.

Here is the corresponding code:

unsigned long int fibo(int n) {
    if (n <= 1)
        return n;
    else
        return fibo(n - 1) + fibo(n - 2);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189