1
$\begingroup$

I'd like to have some way to view the time elapsed since the beginning of a long evaluation, viewable while the computation is still evaluating.

My first thought was to use Monitor, but it seems to require a variable's value to update before updating the display. That is, I can't just provide it an expression I'd like re-evaluated 'continuously' throughout the computation:

With[{start=Now},
    Monitor[ 
        Table[Length[FactorInteger[2^n - 1]], {n, 50, 450, 50}],
        DateDifference[start, Now, "Second"]
    ]
]

Failed attempt -- timing value doesn't update

(Example computation slightly modified from the first example in the docs for Monitor.) During evaluation, the Monitor cell never updates from its initial value -- see above.

Fine -- looks like I was expecting Monitor to do something it doesn't. One would expect it to be difficult for such a function to determine how often to re-evaluate the expression to be monitored anyway.

Then the question becomes whether there's a way to do what I wanted Monitor to do -- evaluate an expression 'continuously' and display its updated value.

Of course, I don't really want this function updated continuously -- just every second or 10th of a second or so, ideally with a configurable duration. Obviously I don't want this monitoring to slow down my computation in any non-neglible sense.

Note in particular that I do not wish to modify the expression being monitored to provide any sort of report on its progress, or anything like that. I want this method to track the evaluation of an arbitrary expression.

$\endgroup$
3
  • $\begingroup$ Related/duplicate: mathematica.stackexchange.com/questions/133182/… $\endgroup$
    – Michael E2
    Commented Mar 27, 2018 at 20:17
  • $\begingroup$ @MichaelE2 Your answer there completely solves the X in my XY problem (+1 -- it's what I ended up using), but I like that CarlWoll solves my 'Y' issue with Monitor. $\endgroup$
    – jjc385
    Commented Mar 28, 2018 at 15:51
  • $\begingroup$ The funny thing is that I was using a variant in this answer (PrintTemporary@Dynamic@{mr, Clock[Infinity]}; about halfway down), and just after posting it, I saw your Q&A. $\endgroup$
    – Michael E2
    Commented Mar 28, 2018 at 16:23

1 Answer 1

4
$\begingroup$

Maybe you can use a Dynamic with an UpdateInterval:

SetAttributes[timed,HoldAll]
timed[expr_] := With[{start = AbsoluteTime[]},
    PrintTemporary @ Dynamic[AbsoluteTime[]-start, UpdateInterval->.1];
    expr
]

Example:

enter image description here

$\endgroup$
1
  • $\begingroup$ Even though MicaelE2's linked answer's use of Clock is what I ultimately went with, I really like the way PrintTemporary and Dynamic with UpdateInterval does exactly what I was hoping Monitor would do. This is useful to me. $\endgroup$
    – jjc385
    Commented Mar 28, 2018 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.