I want to print a timer that starts at 0 when an evaluation begins and stops when the evaluation is completed. I have come up with the following solution using Clock
and Dynamic
but it's a bit clumsy.
time = AbsoluteTiming[
clock = Dynamic[Clock[1000]];
Print["Evaluating... Time: ", Dynamic[clock]];
Pause[2];
];
clock = time[[1]];
Furthermore, if I want to time several evaluations one after the other, I have to introduce new variables time1
time2
, clock1
clock2
etc. so the previous output doesn't get overwritten, which is annoying.
Is there a better way to do this?
Alternatively, I would be happy to only print the evaluation time after the computation has finished. But then I can't get the time to print on the same line as "Evaluating..." becuase Print
always creates a new line. The solution would be to use WriteString
instead of Print
, but I like the Style
and Dynamic
functionalities of Print
, which WriteString
does not support.
Reply to Domen's comment:
In the end I guess the purpose is largely aesthetic. I have looked into Monitor
and PrintTemporary
but have not found them useful for this task (of course I could be wrong about that). Say I'm computing a series expansion with Series
that takes a long time to finish. Then I would like to print some text when the computation starts (for example, "Starting series expansion... "). Then, when the computation is finished, I would like to add the evaluation time to this print output such that it reads "Starting series expansion... Time: 10.234" if it took 10.234 seconds. A nice (purely aesthetic) bonus would be if the timer could be shown in real-time, starting at 0 seconds and stopping at 10.234.
As I said above, printing the evaluation time after the evaluation has finished can be done using WriteString
, but I wonder if there is a solution using Print
.
Monitor
andPrintTemporary
? $\endgroup$