Created
July 10, 2012 16:56
-
-
Save AngeloStavrow/3084683 to your computer and use it in GitHub Desktop.
Check function execution time by calling clock()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clocktest.cpp | |
// ClockTest | |
// | |
// Created by Angelo Stavrow on 10 July, 2012 | |
#include <time.h> | |
#include "iostream" | |
using namespace std; | |
// Calculate the factorial of n recursively. | |
unsigned long long recursiveFactorial(int n) { | |
// Define the base case. | |
if (n == 1) { | |
return n; | |
} | |
// To handle other cases, call self recursively. | |
else { | |
return (n * recursiveFactorial(n - 1)); | |
} | |
} | |
int main() { | |
int n = 60; | |
unsigned long long result; | |
clock_t start, stop; | |
// Mark the start time. | |
start = clock(); | |
// Calculate the factorial of n; | |
result = recursiveFactorial(n); | |
// Mark the end time. | |
stop = clock(); | |
// Output the result of the factorial and the elapsed time. | |
cout << "The factorial of " << n << " is " << result << endl; | |
cout << "The calculation took " << ((double) (stop - start) / CLOCKS_PER_SEC) << " seconds." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment