Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In lisp how can I measure and capture the time spent evaluating an expression?

I want to capture the results of a call to the time macro in order to gather multiple measurements and process them. I tried to locally setf the standard output and redirect it to a string but it didn't work with the time macro. Maybe it is wrong, but what I tried is:

(with-output-to-string (str)
    (let ((*standard-output* str))
        (time (test-with-size 40))))

The questions:

  1. Is a way to capture the output of time?
  2. If not, can I capture the slime-profile-report command's output?
  3. If none of the above works, how can I measure the time spent evaluating an arbitrary expression?

What I want to accomplish is to measure the run-time of an algorithm as the size of the input increases so for each input size (ranging from 1 to 100) I will measure a lot of times and keep the average. Then I want to plot the results. Plotting is easy, and I have found many ways in Cliki, but how can I gather the results?

I am using CLISP and CCL.

EDIT: Paul Nathan pointed that the time macro outputs to *trace-output* which is a solution. I would like a nicer, simpler, solution though, because with this one, I have to parse an implementation specific trace.

like image 505
Paralife Avatar asked Feb 16 '11 18:02

Paralife


1 Answers

If you want to capture the text, you need to use the right stream. The ANSI CL standard says that TIME prints to trace output.

So this would give you the text as a string.

(with-output-to-string (*trace-output*)
  (time (sin 40.0)))

You could also write your own macro using the time primitives. See 25.1.4.3 Internal Time to get numeric data.

like image 151
Rainer Joswig Avatar answered Nov 14 '22 12:11

Rainer Joswig