Profiling

February 28, 2014

Bentley wrote in C and used an execution-count profiler that counted the number of times each statement in his program was executed. Scheme provides no standard profiler, so we have to build our own. Ours is very simple: it provides calling counts of functions that are made known to the profiler. We begin with a global variable to hold the profile counts and a function to reset it when desired:

(define *profile* (list))

(define (reset-profile)
(set! *profile* (list)))

The heart of our profiler is syntax that replaces the normal define syntax with a profiling version. Each time a profiled function is called its name is consed onto the *profile list:

We organize our program a little bit differently than Bentley. Since we can only count function calls, not individual expressions, we separate the trial division into its own function. And we count the primes instead of making a list of them. Here’s what it looks like: