start by collecting your results in a single List instead of in Arrays. that’ll give you a massive increase in performance.
a List is what you want for anything that’s growing. see its helpfile for a bit of explanation and how it compares to Array.
( //~8.5sec
{
var a = [1, 2, 3, 4, 5, 6, 7, 8];
~b = (a.size.factorial).asInteger.collect{arg i; a.permute(i)};
~b = ~b.flatten.asStream;
}.bench;
)
( //~0.09sec
{
var a = [1, 2, 3, 4, 5, 6, 7, 8];
var size = (a.size.factorial).asInteger;
var result = List.new(size);
size.do{arg i; result.addAll(a.permute(i))};
~b = result.asStream;
}.bench;
)
then, if/when you run into new problems, you can split up your code into blocks using a Routine. a .wait now and then will give the Interpreter some time for itself to collect garbage and what not.
(
Routine.run({
var a = [1, 2, 3, 4, 5, 6, 7, 8];
var size = (a.size.factorial).asInteger;
var result = List.new(size);
size.do{arg i;
if(i%10000==0, {
"breathe! collected % numbers".format(result.size).postln;
0.1.wait;
});
result.addAll(a.permute(i));
};
~b = result.asStream;
"done".postln;
result.size.postln;
});
)
good luck,
_f
21 juli 2021 kl. 08:30 skrev lioness via scsynth <noreply@m.scsynth.org>:
| lionqueen
July 21 |
I’m trying to generate arrays containing all possible permutations of n elements. That I got, the problem is that when n gets to, say 10, that’s already above 3.6 million permutations, which means roughly 36 million indices, so my computer (2020 MBP) starts spinning its fans like crazy and that’s about it. n = 11 is nearly 40 million sets, and so on, so it gets much worse
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
~b = (a.size.factorial).asInteger.collect{arg i; a.permute(i)};
~b = ~b.flatten.asStream;
Is there a way to calculate/collect that in blocks, making sure each block does not exceed whatever value seems reasonable for array size? that way i could (hopefully) load block-arrays rather than the supermassive one that seems very unmanageable.
Thanks!
#|
fredrikolofsson.com musicalfieldsforever.com