You must be logging a lot if it can affect performance.
The simplest approach to logging is to have a log predicate somewhere like you have:
Code: Select all
class log
properties
logOn : boolean.
logstream : outputStream.
predicates
log : (...).
logf : (string Format [formatString], ...).
end class log
And a corresponding implementation:
Code: Select all
implement log:
facts
logOn : boolean := false.
logstream : outputStream := stdio::getOutputStream().
clauses
log(...) :
if true = logOn then
logstream:write(...)
end if.
clauses
logf(Format, ...) :
if true = logOn then
logstream:writef(Format, ...)
end if.
end implement log
You would use it like this:
Code: Select all
clauses
p() :-
foreach X in getX_nd() do
...
log::logf("X is %\n", X),
...
end foreach.
If the overhead of the call is too expensive you could perform the check inline:
Code: Select all
clauses
p() :-
foreach X in getX_nd() do
...
if true = log::logOn then
log::logf("X is %\n", X)
end if,
...
end foreach.
I am not sure if that is faster though, but it will be faster if you do like this:
Code: Select all
clauses
p() :-
LogOn = log::logOn,
foreach X in getX_nd() do
...
if true = LogOn then
log::logf("X is %\n", X)
end if,
...
end foreach.
Because then you only access the property once before the loop.
If you want the absolute highest performance you will need some kind of conditional compilation.
This can be achieved by changing the logOn property in to a constant instead. If logOn is a constant then this code will log if it is defined to true, and it will not log if it is defined to false.
Code: Select all
clauses
p() :-
foreach X in getX_nd() do
...
if true = log::logOn then
log::logf("X is %\n", X)
end if,
...
end foreach.
When it is false you will also get a warning about unreachable code, indicating that the compiler have completely removed the code.