1

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
jjc385
  • 3,473
  • 1
  • 17
  • 29
  • Related/duplicate: https://mathematica.stackexchange.com/questions/133182/timing-evaluation-times-dynamically – Michael E2 Mar 27 '18 at 20:17
  • @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. – jjc385 Mar 28 '18 at 15:51
  • 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. – Michael E2 Mar 28 '18 at 16:23

1 Answers1

4

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 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. – jjc385 Mar 28 '18 at 15:46