18
$\begingroup$

A typical example: I type the expression

DumpSave["/some/path/dump.mx", something]

...and hit Shift-Enter.

Then the kernel goes off on a 15-minute evaluation.

Hmmm, that must have been a huge file. Let's see... Nope, it's only 250Mb!.

If I evaluate the same expression but suppress all output (by appending ; to it), the evaluation is over instantly.

IOW, pretty much all of that 15-minute wait was spent on formatting output that I didn't really need.


Output is always nice to have (which is why I'm not in the habit of always appending a ; to every expression I evaluate in the front end), but only as long as it doesn't take too much screen space, or take too long to format or print out.

Recent versions of Mathematica do a good job of limiting the output's screen footprint by default, which is great.

Is there something similar for limiting the time spent on formatting an evaluation's output?

Hopefully, one could override this limit as needed; e.g.

Block[{$MaxOutputFormattingTime = Infinity}, someExpression]

(I spent some time scanning options in the Options Inspector, unsuccessfully; what I'm looking for may very well be somewhere in there, but I was not able to spot it.)

$\endgroup$
1
  • $\begingroup$ The core issue with this is in how I think the FormatValues are applied. The front-end sees boxes and then applies FormatValues to each box form. Thus the front-end doesn't expect to have to elide any data. All elisions should be happening before the boxes are made. The elision in big output comes as a byproduct of the default post-to-notebook action, not the box formatting. For instance, if you run Table[1, 1000, 1000] // Print it does not elide. All of this is a long way to say that I don't think this is possible, although I really wish it were. $\endgroup$
    – b3m2a1
    Commented Oct 7, 2017 at 16:31

1 Answer 1

5
+100
$\begingroup$

Usually by the time the front end gets something, aborting or interrupting interactively is useless, so I won't suggest that. IF I suspect a problem beforehand, I would use Short or something similar. For the type of question you are posing, I would try the Villegas-Gayley pattern to inject a high priority DownValue into MakeBoxes, flagging on something useful like the ByteCount.

Module[{c=0,r,t=10000},
    MakeBoxes[e_,f___]/;c===0 := Block[{c=1,r},
        c=ByteCount[r=MakeBoxes[e,f]];
        If[c<t,r,MakeBoxes[Short@e,f]]
    ]
]

Table[1,{60}]
Table[1,{80}]

(*{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}*)

(*{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,<<45>>,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}*)

By request, here is another version that will shorten the output if TimeConstrained cuts off Rasterize after 1 second. The example writes 1000 graphic objects to the notebook, but with the modification to MakeBoxes, 930 of them are omitted, significantly speeding up the display.

Module[{c=True,r,t=1},MakeBoxes[e_,f___]/;c:=Block[{c=False,r},r=TimeConstrained[Rasterize[Unevaluated@e],t];If[r=!=$Aborted,MakeBoxes[e,f],MakeBoxes[Short@e,f]]]]
Graphics[{RGBColor @@ RandomReal[1, 3], Disk[RandomReal[{-5, 5}, 2], RandomReal[]]} & /@ Range[100], ImageSize -> 40*{5, 5}] & /@ Range[1000]
(*1000 pictures output similar to below, but 930 of them are omitted by Short*)

enter image description here

$\endgroup$
8
  • $\begingroup$ IF is suspect a problem beforehand, there is no problem: I simply add ; at the end of the expression, and that's the end of it. The problem only arises, precisely, when I do not suspect a problem beforehand, and end up stuck with an effectively frozen front end. Also, the implementation of the front end could easily build a time-out into the rendering step. This does not strike me as a technically difficult feature. $\endgroup$
    – kjo
    Commented Oct 9, 2017 at 17:15
  • $\begingroup$ The main problems I have with this solution are: 1) it's not a setting one can make once and forget about it (though maybe this can be fixed); 2) it relies on ByteCount as a proxy for rendering time, and, as I argue in my post, size seems to be a very poor indicator of rendering time. $\endgroup$
    – kjo
    Commented Oct 9, 2017 at 17:49
  • $\begingroup$ The idea of monkey-patching MakeBoxes is an interesting one, though. What's needed is a way to implement an asynchronous time-out on it. $\endgroup$
    – kjo
    Commented Oct 9, 2017 at 17:54
  • $\begingroup$ (Unfortunately, I don't have access to Mathematica at the moment, so I can't test these things.) $\endgroup$
    – kjo
    Commented Oct 9, 2017 at 17:58
  • $\begingroup$ Sorry, I posted this issue 6 months ago, but I no longer have access to Mathematica (which I got through my employer then), so unfortunately I can't provide a minimal example. $\endgroup$
    – kjo
    Commented Oct 9, 2017 at 21:25

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.