SWI-Prolog offers a statistical program profiler similar to Unix prof(1) for C and some other languages. A profiler is used as an aid to find performance pigs in programs. It provides information on the time spent in the various Prolog predicates.
The profiler is based on the assumption that if we monitor the
functions on the execution stack on time intervals not correlated to the
program's execution the number of times we find a procedure on the
environment stack is a measure of the time spent in this procedure. It
is implemented by calling a procedure each time slice Prolog is active.
This procedure scans the local stack and either just counts the
procedure on top of this stack (plain
profiling) or all
procedures on the stack (cumulative
profiling). To get
accurate results each procedure one is interested in should have a
reasonable number of counts. Typically a minute runtime will suffice to
get a rough overview of the most expensive procedures.
Profiling in the Windows version is similar but, especially on Windows 95/98/ME one should be aware of the implementation. (43) Windows does not provide timers that fire asynchronously, frequent and proportional to the CPU time used by the process. Windows does provide multi-media timers that can run at high frequency. Such timers however run in a seperate thread of execution and they are fired on the wall-clock rather than the amount of CPU time used. The profiler installs such a timer running, for saving CPU time, rather inaccurately at about 100 Hz. Each time it is fired, it determines the millisecons CPU time used by Prolog since the last time it was fired. If this value is non-zero, active predicates are incremented with this value.
On Windows 95/98/ME (DOS-based Windows), there is no possibility to get the CPU-time used by a thread or process. Therefore, on these systems profiling results does not count CPU-time, but elapsed time. For sensible results on these systems, ensure the system has no other active tasks and be aware that I/O operations include the time Prolog is blocked waiting for data.
off
, plain
or cumulative
. plain
implies the time used by children of a predicate is not added to the
time of the predicate. For status cumulative
the time of
children is added (except for recursive calls). Cumulative profiling
implies the stack is scanned up to the top on each time slice to find
all active predicates. This implies the overhead grows with the number
of active frames on the stack. Cumulative profiling starts debugging
mode to disable tail recursion optimisation, which would otherwise
remove the necessary parent environments. Switching status from
plain
to cumulative
resets the profiler.
Switching to and from status off
does not reset the
collected statistics, thus allowing to suspend profiling for certain
parts of the program.
off
and clears all collected
statistics.
cumulative
) or on top of
the stack (plain
). Promilage is an integer
between 0 and 1000.