15
$\begingroup$

I've came upon a few methods of how to display "processing" information while Mathematica is running. But, from what I've understood, this only works in between lines of codes. Is it possible to display a "how far is something being processed"-information during a line of code?

For example, I wish to Map a function g over a very large list, for example:

Map[ g , Sort[Apply[Join, Table[Range[1000000], {1000}]]] ],

which will take quite some time. Now, because the list consists of 1000 times every number between 1 and 1000000, could it be possible do display (as a temporary display), which number is being processed?

I have thought of using Print and changing the function being mapped to

Map[ (Print[#]; g[#]) &, Sort[Apply[Join, Table[Range[1000000], {1000}]]] ],

but I was wondering if there was any "faster" method, which wouldn't print out every number of the list, but for example every 1000th number. I have thought about If before Print, but I think this method would take too much extra time.

$\endgroup$
2
  • 2
    $\begingroup$ See this Q&A for some ideas. $\endgroup$
    – cormullion
    Commented Oct 31, 2013 at 10:25
  • $\begingroup$ Thanks for the link, I like these ideas! $\endgroup$
    – Gabriel
    Commented Oct 31, 2013 at 14:04

3 Answers 3

9
$\begingroup$

Or you can use something like this:

Monitor[
    Table[Length@FactorInteger[2^n - 1], {n, 50, 500, 50}], 
    Grid[{{Text[Style["Integer Factorization :", Darker[Blue, 0.66]]],
        ProgressIndicator[n, {50, 500}]},
    {Text[Style["Factoring 2^n-1 with n : ", Darker[Blue, 0.66]]], n}},
Alignment -> Left, Dividers -> Center]]

enter image description here

Sorry for not using your computation, since it did not make sense to me, which is my fault. That's why I've chosen another, just to display my approach.

$\endgroup$
9
$\begingroup$

A simple way to do it would be to partition the big list into chunks, and map over each chunk in turn, monitoring the progress of that outer loop.

data = Range[10^6];
chunks = Partition[data, 1000];
result = Flatten[Table[g /@ chunks[[i]], {i, Length[chunks]}]] ~Monitor~ i;

Short[result]
(* {g[1], g[2], g[3], g[4], <<999992>>, g[999997], g[999998], g[999999], g[1000000]} *)
$\endgroup$
2
  • $\begingroup$ Thanks! What do you mean by Length[pd]? Should this be Length[chunks]? $\endgroup$
    – Gabriel
    Commented Oct 31, 2013 at 13:28
  • $\begingroup$ @Gabriel, yes it should. Sorry, I changed the symbol name to make it clearer but missed one! $\endgroup$ Commented Oct 31, 2013 at 14:29
4
$\begingroup$

Provided you can put your intermediate results or progress indicator into a common scope with a ScheduledTask you can monitor proceedings as follows...

Module[{x = 0, monitor},
 (* Create a monitoring ScheduledTask *)
 StartScheduledTask[monitor = CreateScheduledTask[PrintTemporary[x], 1]];

 (*SomeBigCalculation*) 
 Do[x++, {10^9}];

 (*Kill the monitor when not required.*)
 StopScheduledTask[monitor];
]

What the monitor task does of course is up to you.

$\endgroup$
5
  • 2
    $\begingroup$ Or you could simply write Dynamic@x. $\endgroup$ Commented Oct 31, 2013 at 14:52
  • $\begingroup$ István - I was skeptical but using Dynamic is impressively fast. I assume it only writes its value to the display occasionally, not on every update. $\endgroup$
    – Ymareth
    Commented Oct 31, 2013 at 16:13
  • 1
    $\begingroup$ You can specify the update speed with Dynamic[Refresh[x, UpdateInterval -> int]], otherwise "Dynamic[expr] evaluates expr whenever it needs its value and it determines that any type of value of symbols on which it depends might have changed." (from the docs), which might mean low time granularity. $\endgroup$ Commented Oct 31, 2013 at 16:50
  • $\begingroup$ But of course a ScheduledTask is very useful, did not mean to devalue your answer, I use it a lot! $\endgroup$ Commented Oct 31, 2013 at 16:51
  • $\begingroup$ None assumed : no dueling pistols drawn :) $\endgroup$
    – Ymareth
    Commented Oct 31, 2013 at 18:04

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.