I am writing a logfile and it is getting pounded. I thought of reading a debug(1) internal db from a file to control whether the log is written to or not by:
consult debug(1) then in loop call....
log (_Msg) :- debug(1),!.
log (_Msg) :- write_log(_Msg).
with memory being so slow compared to cpu is there a faster running way to do this?
if conditional compile must be recompiled for debug but blazes i would use that.
Thanks
AEH
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
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:
And a corresponding implementation:
You would use it like this:
If the overhead of the call is too expensive you could perform the check inline:
I am not sure if that is faster though, but it will be faster if you do like this:
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.
When it is false you will also get a warning about unreachable code, indicating that the compiler have completely removed the code.
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
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
Code: Select all
clauses
p() :-
foreach X in getX_nd() do
...
log::logf("X is %\n", X),
...
end foreach.
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.
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.
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.
Regards Thomas Linder Puls
PDC
PDC