1

We have a long Do loop for our program in which we have put a counter to see the stage of process.

counter=0;Do[

  a program; counter+=counter;
  Print[counter]
  ,{i,1,10^6}];

But we do not want to have a cell devoted to each counter when it is printed. If this happens, we will have 10^6 cells below each other which consume a huge space in a notebook of Mathematica. Actually we wish to have a temporary printing process. Of course, the running program takes a long time to be done and we do not need Pause[nSeconds] that is emphasized in the Mathematica documents. We just want to see temporarily the counter once the program finished in every iteration! We have seen this link and this link but they do not work for us efficiently.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Unbelievable
  • 4,847
  • 1
  • 20
  • 46

2 Answers2

3

Check the Documentation page for Monitor. Here is a quick example:

program := Pause[.2]
Monitor[Do[program, {i, 1, 40}], i]

Of course, the running program takes a long time to be done and we do not need Pause[nSeconds] that is emphasized in the Mathematica documents.

Pause[nSeconds] is just a stub for a program that takes significant time to compute, nothing more. Replace it with a call to your actual program!


Related:

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
1

You can use a file to decide if you want the program to print variables or not. For instance, only if the file exists, the program print variables which you are interested in.

It's tested on macOS, it should work on Linux as well.

  1. Evaluate the cell
  2. Execute the shell command whenever you want to check the status.

In:

Mathematica

ClearAll[debug]
debugQ[] := FileExistsQ["debug.txt"]
debugQ[]
Do[(Which[debugQ[], Print@i]; i), {i, 1, 10^8}]

Shell

echo 1 > debug.txt;rm debug.txt

Out:

enter image description here

webcpu
  • 3,182
  • 12
  • 17
  • Please explain what does the shell command echo 1 > debug.txt;rm debug.txt do exactly? Is it for Windows or for Linux? – Alexey Popkov Jun 02 '17 at 13:14
  • 2
    It might work on windows if you can use Bash on Windows. the shell command creates debug.txt and then delete it. During the short period of time, the program will print debug messages when the program detects that debug.txt exists. – webcpu Jun 02 '17 at 13:27
  • @UnchartedWorks Thanks, I assumed that it should be so but when run using cmd on Windows this command just creates the file "debug.txt" with contents "1 ;rm debug.txt". – Alexey Popkov Jun 02 '17 at 13:30
  • If the program takes long time to evaluate, it simply may not notice the quick creation-deletion procedure, which consequently won't have any effect. – Alexey Popkov Jun 02 '17 at 13:33
  • @AlexeyPopkov dir > debug.txt & del debug.txt – webcpu Jun 02 '17 at 13:34
  • @AlexeyPopkov Not really, Front end won' t evaluate any new expression before the current expression is evaluated. – webcpu Jun 02 '17 at 13:36
  • Yes, but it isn't related to debugQ[] which is evaluated in the Kernel. – Alexey Popkov Jun 02 '17 at 13:39
  • @ AlexeyPopkov It depends on how often the program checks the file. If the interval of checking the file is long, you can just create the file and don't delete it immediately. – webcpu Jun 02 '17 at 13:39
  • That's what I talked about: this approach is only reliable when debugQ[] is evaluated very often. – Alexey Popkov Jun 02 '17 at 13:40
  • It's possible to use a new front end to send a new expression to the same kernel, however the setup is complicated. I think it's unnecessary in this case. – webcpu Jun 02 '17 at 13:42
  • I would rather say it's on demand. It's like interrupt but not polling. – webcpu Jun 02 '17 at 13:44
  • The idea of your answer is interesting but IMHO has narrow applicability. In general it is simpler to create a Palette with a button which will turn on printing of (for example) 10 debug messages. – Alexey Popkov Jun 02 '17 at 13:45