bluss / permutohedron

https://docs.rs/permutohedron/
Apache License 2.0
38 stars 5 forks source link

Code hangs on generating permutations #7

Closed agmcleod closed 7 years ago

agmcleod commented 7 years ago

Running 0.2.2, it just seems to hang, not sure why. Trying where i used this for advent of code in 2015, it runs fine, though that seems to be using Vec<&str>, where here i'm using Vec<usize>

extern crate permutohedron;
use permutohedron::Heap;

fn main() {
    let mut data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    let heap = Heap::new(&mut data);

    let mut permutations = Vec::new();
    for data in heap {
        permutations.push(data.clone());
    }

    println!("{:?}", permutations);
}
bluss commented 7 years ago

Hi. On my machine this program will either "hang" (debug mode, it's slow) or in release mode the operating system will kill it from using too much memory.

Note that there are 12! = 479 001 600 permutations, so it will need a lot of memory, around 60 GB on 64-bit; each Vec uses a 96-byte allocation and 24 bytes for itself! This code doesn't make sense to me, the program can't handle so many elements. I suggest you generate a smaller set of permutations.

agmcleod commented 7 years ago

Ah okay, yeah that makes sense. I realized it wasn't what i quite needed anyways, i needed permutations of each value by 2, not by every in the array. So was able to build that with a couple nested loops and a contains() check.