Wait a sec, I just realized that your function takes parameter
n
and then waits for a
cin>>n
.
First, your performance measurements will really only measure the time it takes to cin>>n as your typing will take longer than any computer operation.
Second, the point of passing n as an argument is defeated if you just write to it in the function.
Remember, functions should do their job and ONLY their job. It should calculate the factoral only, not handle inputs. Any additional logic in there will skew your analysis.
Try this instead:
1 2 3 4 5 6 7 8 9 10

int main() {
struct timeval stop, start;
int n;
cin >> n;
gettimeofday(&start, NULL);
factorial(n); // Calling the function
gettimeofday(&stop, NULL);
cout << "Time: " << stop.tv_usec  start.tv_usec << endl;
return 0;
}
 
Then
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

//Iterative Version
unsigned int factorial(unsigned int n)
{
int tot = 1;
for(int i = 1; i <= n; i++)
tot *= i;
return tot;
}
//Recursive Version
unsigned int factoral_recursive(unsigned int n)
{
if (n < 2) return 1;
return n*factoral_recursive(n1);
}
 
Third, unsigned int only has 32 bits on most systems. That's only going to get you to 12! which is only 12 iterations before overflow. That isn't many calculations to analyze. You could use unsigned long long (64 bits), but that will only get you to 15! For performance analysis, choose something that can handle tons of iterations like a square root, addition, fibinacci sequence, taylor series or something that doesn't explode on us.