Mathematica Table function

后端 未结 3 765
臣服心动
臣服心动 2020-12-31 10:38

I\'m running a Table function which will take too much time to complete.

I wanted to know if there\'s a way to retrieve the results computed so far.

3条回答
  •  灰色年华
    2020-12-31 10:57

    Unfortunately no. If you want to do something like lst=Table[f[i],{i,1,10000}] so that if aborted you still have results, you could do

    Clear[lst2];
    lst2 = {};
    (Do[lst2 = {lst2, f[i]}, {i, 1, 10000}];
    lst2=Flatten[lst2];) // Timing
    

    which, for undefined f, takes 0.173066s on my machine, while lst = Table[f[i], {i, 1, 100000}] takes roughly 0.06s (ie, Table it is 3 times faster at the expense of not being interruptible).

    Note that the obvious "interruptible" solution, lst = {}; Do[AppendTo[lst, f[i]], {i, 1, 100000}] takes around 40s, so don't do that: use linked lists and flatten at the end, like in my first example (however, that will break if f[i] returns a list, and more care is then needed).

提交回复
热议问题