That profile is an HTML file using Bootstrap and other fun things to make it look pretty. It’s not as sophisticated as something like Perl 5’s Devel::NYTProf, but it’s still pretty interesting.

Let’s write some routines to do Fibonacci numbers, which seem to be most authors’ favorite example for this sort of thing. Indeed, I used it in the “Benchmarking” chapter of Mastering Perl, but mostly to point out how wrong the simple benchmarks were.

I’ll implement it in four ways, none of which I claim to be the best:

The recursive way you see in most examples

A recursive fashion with memoization

Using the sequence operator

Using memoization with an iterative approach.

In Mastering Perl, I presented a few arguments that escape the usual discussion:

The routine that runs fastest the first time isn’t necessarily the best

Caching results you’ll use again eventually pays off in time

Using less time usually means using less memory

You improve with better algorithms instead of different syntax.

Here are some implementations in Perl 6, where I didn’t try at all to make them efficient. I merely want something to show the workings of the profiler:

I can then look at the profiler output. Here’s a screenshot that shows the time spent in each of the subroutines:

To see the rest of the report, run the program yourself!

I won’t write too much about interpreting these results, but there are several things you should think about in these sorts of comparisons.

First, test with a variety of values, especially those of vastly different orders of magnitude. The recursive solution might look really fast for small N, but it gets bad very quickly.

Second, test it several times, as I’ve done in this example. Some implementations always take the same amount of time while others get faster and faster. In a program where this level of optimization matters, you’re likely to run the interesting routine several times.

Third, consider a completely different approach than any you see here. The Fibonacci numbers aren’t going to change (neither are the values of other sequences). Why compute them again? Just look them up. (I’ll write more about this tomorrow when I use multi subs) Something like Redis can store huge lists without eating all your resources or complicating your program.