'C help: Using clock_gettime more than one seemingly keeps returning 0.000001

I'm trying to code a part of a program that uses clock_gettime to record the runtime of two different calculating functions. One of them prints the calculation time for each iteration of a number with no issue - the time increases the higher the input value, n, becomes.

However, for a second function, it keeps printing 0.000001 on the way down. I've checked my code repeatedly; and i'm not sure why it keeps printing this way. My code could be okay, and the output is to be expected, but I am not too confident on that possibility.

I will disclose that this is for an assignment i'm doing for a class, and a significant portion is already complete. This is the only part that i'm just stuck on and I want to figure out what it is i'm missing to result in this value constantly being printed.

Edit: Someone pointed out that it was printing in microseconds and not nanoseconds - so I had to increase the amount of decimal places that could be printed. That started giving me the proper output.

    for(i = 0;i <= n; i++)
    {
        //func_a
        clock_gettime(CLOCK_REALTIME, &start);
        func_a(i);
        clock_gettime(CLOCK_REALTIME, &end);

        nr_runtime = (end.tv_sec - start.tv_sec)
                + (end.tv_nsec - start.tv_nsec)/1000000000.0;

        //----------------------------------------------

        //func_b
        clock_gettime(CLOCK_REALTIME, &start);
        func_b(i);
        clock_gettime(CLOCK_REALTIME, &end);

        nr_runtime = (end.tv_sec - start.tv_sec)
                + (end.tv_nsec - start.tv_nsec)/1000000000.0;
     }

The functions:

int func_a(int n) //recursive fibonacci
{
    int i; 
    
    if (n == 0 || n == 1)
    {
        return n; 
    }
    else
    {
        return fib(n-1) + fib(n-2); 
    }
}

int func_b(int n) //Iterative fibonacci
{

    int a=0, b=1, c;

    if (n == 0 || n == 1)
    {
        return n; 
    }

    else
    {
        int i;

        for(i=2; i<=n; i++)
        {
            c = a + b; //next term
            a = b;
            b = c;
        }

        return c;
    }
}

Output for func_a:

actual numbers like 0.00234 to 1.73564

Output for func_b:

0.000001 going onward until value of n is reached

I checked my functions and both of them output the same calculated values using different methods of calculation - so I know that part is fine...which is why I'm so confused.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source