# Mathematica is getting slow when repeatedly calling an external program

I found a strange phenomenon that Mathematica is getting slow when I repeatedly called an external program by using ReadList. For example, the following code will display a significant increase of CPU time for each 'ls' command (my platform is OS X):

data = Table[Timing[ReadList["!ls"]], {100000}][[All, 1]];
ListPlot[data]


This phenomenon seems ubiquitous for the other external programs. Can anyone suggest me how to avoid this speed down?

• Is this actually a problem or just an annoyance? When you say other external programs do you include ones with more significant IO than a simple ls? Additionally does it do the same if you call ls through a shell script? Ie are you sure it's MMA and not just OSX? – Gordon Coale Jun 25 '15 at 6:00
• Confirmed same issue on my MBA running Mavericks and MMA10.1. One point to note is my results for same code are 10 times faster i.e. 0.002 to 0.006 although the trend is very similar. This may just be due to directory contents, or maybe my SSD? – Gordon Coale Jun 25 '15 at 8:47
• Might be a good idea to send a bug report to WRI and see what they have to say about it. – C. E. Jun 25 '15 at 11:37
• I had to change the code to the following: data = Table[Timing[Do[ReadList["!dir", "String"], {250}]], {100}][[All, 1]]; ListPlot[data, PlotRange -> All]. However, with this I could not reproduce your observations on Windows. The timings appeared scattered in an essentially random way. – Oleksandr R. Jun 25 '15 at 12:23
• Perhaps some stream or channel is not being closed properly with this code under OS X? – Mr.Wizard Jun 25 '15 at 14:08