0

I am trying to create a method that generates all possible combinations of letters within a limit set by the user. I have tried using a simple recursive nested foreach loop, I tried the same using parallel iterators, and this is my latest attempt which is linq with the parallel option set. So far this is probably the fastest method, but it still leaves something to be desired in terms of speed. I am just curious if anyone knows of any further methods to speed up this process?

This is the code at the moment, which is just the parallel linq inside of a worker, so the form can be updated of the progress.

It currently generates around: 11,881,376 combinations per minute

  BackgroundWorker bw = new BackgroundWorker();

  bw.DoWork += new DoWorkEventHandler(
   delegate(object o, DoWorkEventArgs args) {

    var total = 6; // how many characters to generate
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    var q = alphabet.Select(xf => x.ToString());

    for (int i = 0; i < total - 1; i++) {
     q = q.AsParallel().SelectMany(xf => alphabet, (xf, y) => xf + y);
     //bw.ReportProgress(q.Count());
    }

   });

  bw.RunWorkerAsync();
Nobody
  • 341
  • 2
  • 6
  • It depends on how are you going to consume the combination. If you can work with `char[]` instead of a `string`, my answer to [System.OutOfMemoryException when generating permutations](http://stackoverflow.com/questions/35385506/system-outofmemoryexception-when-generating-permutations/35527854#35527854) is so far the fastest way I've ended up. – Ivan Stoev Mar 18 '16 at 15:59
  • What I am generating is simply a plaintext password list, which will later be hashed according to the users settings (md5, sha1, sha256, etc). I don't suppose there's any reason I couldn't use chars for this? Thank you for the link btw, I think that should point me in the right direction at least. edit: Reading your post, I just noticed when running this code my processor/ram usage is rather high. Ram usage is sitting around 6gb out of my 8total. I have not ran this code for more than 5 minutes, so I would have probably ended up running into the out of memory error! – Nobody Mar 18 '16 at 16:17
  • Eric Lippert has [a series of blog posts on producing permutations and combinations](http://ericlippert.com/tag/permutations/), which might help. – Richard Deeming Mar 18 '16 at 18:39

0 Answers0