How can I write output of time/1 to a textfile?

21 views
Skip to first unread message

Chris McMaiersen

unread,
Dec 12, 2014, 4:40:52 PM12/12/14
to swi-p...@googlegroups.com
Hi all,

I need to do performance evaluation for some testcases and I want to use time/1 to measure execution time, which works as expected. My problem is that I can't figure out how to write the output of 'time' to a textfile. I tried to use open/4 but I'm still not sure if it is even possible to do it that way? I read somewhere that using statistics/2 instead of time/1 may help, but I wasn't able to get it to work with that either, moreover I couldn't reproduce my measurement values from time/1 with statistics/2...

Regards,
Chris

Jan Wielemaker

unread,
Dec 13, 2014, 4:01:48 AM12/13/14
to Chris McMaiersen, swi-p...@googlegroups.com
time/1 uses the message infrastructure to print the result. You can hook
that using message_hook/3, but using statistics is a bit easier. The
bottom line is simply

my_time(Goal) :-
statistics(cputime, T0),
Goal,
statistics(cputime, T1),
T is T1-T0,
format('Used ~3f seconds~n', [T]).

Cheers --- Jan

P.s. Attached is a little library I've used several times to create
a LaTeX table.

>
> Regards,
> Chris
>
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.
gctime.pl

Chris McMaiersen

unread,
Dec 13, 2014, 6:13:28 AM12/13/14
to swi-p...@googlegroups.com, cmaier....@gmail.com
Hi Jan,


Am Samstag, 13. Dezember 2014 10:01:48 UTC+1 schrieb Jan Wielemaker:
time/1 uses the message infrastructure to print the result.  You can hook
that using message_hook/3, but using statistics is a bit easier.  The
bottom line is simply

my_time(Goal) :-
        statistics(cputime, T0),
        Goal,
        statistics(cputime, T1),
        T is T1-T0,
        format('Used ~3f seconds~n', [T]).
Thank you very much, this is exactly what I need! If I measure the timings this way they are even more precise than with time/1.
 
        Cheers --- Jan
Regards,
Chris.
 
P.s.        Attached is a little library I've used several times to create
        a LaTeX table.
 Very helpful, integrating all the data in my LaTeX doc is the next step on my agenda! ;-)
Reply all
Reply to author
Forward
0 new messages