As PHP is simpler for me I wished to benchmark algorithms in it for fun, and I chose to do factorials.
The recursive function completely flunked in speed when I got up to 80! compared to the iterative method, and it gradually skyrocketed upwards while iterative had a steady line, actually it is something like this (x = factorial, y = seconds):

But in C/Java (which I just implemented the test in) show the same results to be only 1-5% off from eachother, almost the same speed.
Is it just useless to benchmark algorithms in this manner in scripting languages?
EDIT: For NullUserException:
function factrec($x) {
if($x <= 1) {
return $x;
} else {
return $x * factrec($x - 1);
}
}
It’s absolutely not pointless to benchmark algorithms in a scripting language. After doing the benchmarks, which implementation of factorial would you use in PHP? (assuming that you couldn’t use the builtin one for some reason.)
It is fairly pointless to benchmark in a language that has significantly different features than the one that you want to implement the algorithm in though. Here, the relative cost of function calls and
ifstatements in PHP is skewing the results significantly (or this is my best guess anyways). If you are careful to understand why this is happening and avoid it, it can still be fruitful though: differences will be more exaggerated as you noticed. It comes down to if it’s easier to write it in the target language or work around the differences.A simple calculation of complexity of the algorithm should be enough to decide which one to use, or at least narrow down the selections.
As Mike Axiak points out in the comments, you are not even testing different algorithms here, you are testing two different implementations of the same algorithm: keep a running product over
ifromnto1. Doing this in a different language than the target is almost always pointless.