How to keep building list until manually stopped?
You can use Sow
for this in conjunction with CheckAbort
:
Reap@CheckAbort[Sow[...], ...]
You can read more about this here
Other than that there is bags. It will be as fast as Sow
since Sow
is implemented in terms of bags, but you will not lose the content of the bag if you interrupt the computation.
Quick recap:
bag = Internal`Bag[{}] (* Create an empty bag *)
Internal`StuffBag[bag, value] (* Add a value to the bag *)
Internal`BagLength[bag] (* The number of values in the bag *)
Internal`BagPart[bag, part] (* Retrieve values from bag *)
Another option is to use linked lists.
Writing into a globally accessable Association
is also an option. I would write to file only
- if there is too much data to keep in memory or
- if the computations take really long and if the risk of a kernel quit is not negliable.