Calculating Execution Time
Mar 19, 2017 at 4:40pm UTC
I am trying to find the execution time for a program that calculates Fibonacci numbers using an iterative algorithm, but I am getting no data - my output is 0 ms/0 clicks. I copied and pasted this code from another program where it works as it is intended to and calculates execution time for a recursive algorithm. What am I missing?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//Function Prototypes
long int fibI(int );
#include <iostream>
#include <time.h>
#include <ctime>
int main()
{
int num = 40; //Number of Fibonacci numbers to genreate
//Begins iterative clock
clock_t beginI = clock();
//Calculates and displays Fibonacci numbers
std::cout << "\nFibonacci Number " << num << ": " << fibI(num) << std::endl;
//Stops recursive clock
clock_t endI = clock();
//Prints number of clicks for execution
std::cout << "\nElapsed Time (Iterative): " << ((double (endI-beginI)/CLOCKS_PER_SEC) * 1000) << " ms\n" ;
std::cout << "\nClicks: " << endI << std::endl;
return 0;
}
/*************************************************
* fibI *
* *
* This function iteratively calculates *
* Fibonacci numbers. Reference: *
* www.codeproject.com/tips/109443, *
* author julian *
* *
* Accepts: integer (position of Fibonacci *
* number in series) *
* *
* Returns: integer (value of specified *
* Fibonacci number) *
*************************************************/
long int fibI (int n)
{
int first = 0;
int second = 1;
int counter = 2;
while (counter < n)
{
int temp = second;
second = first + second;
first = temp;
++counter;
}
if (n == 0)
return 0;
else
return (first + second);
}
Mar 19, 2017 at 5:47pm UTC
computers are fast
Topic archived. No new replies allowed.