log file inclusion speed

Discussions related to Visual Prolog
augustinharrison
Posts: 20
Joined: 28 Jul 2010 1:28

log file inclusion speed

Unread post by augustinharrison » 6 May 2014 21:49

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
aeh

User avatar
Thomas Linder Puls
VIP Member
Posts: 1632
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls » 7 May 2014 10:22

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.
Regards Thomas Linder Puls
PDC

augustinharrison
Posts: 20
Joined: 28 Jul 2010 1:28

log

Unread post by augustinharrison » 9 May 2014 18:42

thank you Thomas
aeh

Post Reply