Skip to content

Instantly share code, notes, and snippets.

@AngeloStavrow
Created July 10, 2012 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngeloStavrow/3084683 to your computer and use it in GitHub Desktop.
Save AngeloStavrow/3084683 to your computer and use it in GitHub Desktop.
Check function execution time by calling clock()
// 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