Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a function doSomethingComplicated[...] that takes about 10s on average to evaluate. My list, listOfArgs has about 10000 elements. I'd like to monitor the progress of Map[doSomethingComplicated, listOfArgs] without having it ever occupying a big area of the screen. What's best way to do that?

share|improve this question

1 Answer 1

up vote 19 down vote accepted

Using MapIndexed, which also gives you the index of the element in the list instead of Map would be very convenient. That way, you can set up a Print or a PrintTemporary cell. For example:

Clear[f]
f = (Pause[0.2]; PrintTemporary[First@#2]; #^2) &; (* your complicated function *)
MapIndexed[f, Range[10]]

(* 1
   2
   ...
   ...
  {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} *)

With a PrintTemporary cell, it prints all the output, but the cell disappears when the evaluation is complete, saving you the trouble of cleaning the mess.

You can also print directly to the messages notebook using the PrintToConsole function from my answer here. This keeps your current working notebook clean and you can always check in the other to see the progress.


Another option is to use side-effects and increment a variable, which allows you to use Monitor. For example:

Clear[g]
g = (Pause[0.2]; i = First@#2 + 1; #^2) &;
Block[{i = 1}, Monitor[MapIndexed[g, Range[10]], i]]

See this answer by Brett Champion for the different ways in which you can create a progress bar to suit your needs.

share|improve this answer
1  
You could write a general function to do this: MapMonitored[f_, args_List] := Module[{x = 0}, Monitor[MapIndexed[(x = #2[[1]]; f[#1]) &, args], ProgressIndicator[x/Length[args]]]] –  Ian Hinder Apr 10 at 7:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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