C # How to Run Application Faster

I am creating a wordlist of possible uppercase letters to prove that this code's unsafe 8-digit passwords would write aaaaaaaa in aaaaaaab to aaaaaaac etc. to zzzzzzzz using this code:

class Program
{
    static string path;
    static int file = 0;
    static void Main(string[] args)
    {
        new_file();
        var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789+-*_!$£^=<>§°ÖÄÜöäü.;:,?{}[]";
        var q = alphabet.Select(x => x.ToString());
        int size = 3;
        int counter = 0;

        for (int i = 0; i < size - 1; i++)
        {
            q = q.SelectMany(x => alphabet, (x, y) => x + y);
        }
        foreach (var item in q)
        {
            if (counter >= 20000000)
            {
                new_file();
                counter = 0;
            }
            if (File.Exists(path))
            {
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine(item);
                    Console.WriteLine(item);
                    /*if (!(Regex.IsMatch(item, @"(.)\1")))
                    {
                        sw.WriteLine(item);
                        counter++;
                    }
                    else
                    {
                        Console.WriteLine(item);
                    }*/
                }
            }
            else
            {
                new_file();
            }
        }
    }

    static void new_file()
    {
        path = @"C:\" + "list" + file + ".txt";
        if (!File.Exists(path))
        {
            using (StreamWriter sw = File.CreateText(path))
            {
            }
        }
        file++;
    }
}

      

The code works fine but takes weeks to run. Does anyone know a way to speed it up or should I wait? If anyone has an idea please tell me.

+3


source to share


3 answers


Performance:

size 3:   0.02s
size 4:   1.61s
size 5: 144.76s

      

Tips:



  • remote LINQ to generate a combination
  • removed Console.WriteLine for every password
  • removed StreamWriter
  • large buffer (128k) for writing files

  const string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789+-*_!$£^=<>§°ÖÄÜöäü.;:,?{}[]";
  var byteAlphabet = alphabet.Select(ch => (byte)ch).ToArray();
  var alphabetLength = alphabet.Length;

  var newLine = new[] { (byte)'\r', (byte)'\n' };

  const int size = 4;
  var number = new byte[size];
  var password = Enumerable.Range(0, size).Select(i => byteAlphabet[0]).Concat(newLine).ToArray();

  var watcher = new System.Diagnostics.Stopwatch();
  watcher.Start();

  var isRunning = true;
  for (var counter = 0; isRunning; counter++)
  {
    Console.Write("{0}: ", counter);
    Console.Write(password.Select(b => (char)b).ToArray());
    using (var file = System.IO.File.Create(string.Format(@"list.{0:D5}.txt", counter), 2 << 16))
    {
      for (var i = 0; i < 2000000; ++i)
      {
        file.Write(password, 0, password.Length);
        var j = size - 1;
        for (; j >= 0; j--)
        {
          if (number[j] < alphabetLength - 1)
          {
            password[j] = byteAlphabet[++number[j]];
            break;
          }
          else
          {
            number[j] = 0;
            password[j] = byteAlphabet[0];
          }
        }
        if (j < 0)
        {
          isRunning = false;
          break;
        }
      }
    }
  }
  watcher.Stop();
  Console.WriteLine(watcher.Elapsed);
}

      

+3


source


Try the following modified code. In LINQPad, it starts up in <1 second. With your original code, I gave up after 40 seconds. It removes the overhead of opening and closing a file for every operation WriteLine

. You will need to test and make sure it gives the same results because I don't want to run your source code for 24 hours to ensure the same output.



class Program
{
    static string path;
    static int file = 0;
    static void Main(string[] args)
    {
        new_file();
        var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789+-*_!$£^=<>§°ÖÄÜöäü.;:,?{}[]";
        var q = alphabet.Select(x => x.ToString());
        int size = 3;
        int counter = 0;

        for (int i = 0; i < size - 1; i++)
        {
            q = q.SelectMany(x => alphabet, (x, y) => x + y);
        }

        StreamWriter sw = File.AppendText(path);

        try
        {
            foreach (var item in q)
            {
                if (counter >= 20000000)
                {
                    sw.Dispose();
                    new_file();
                    counter = 0;
                }
                sw.WriteLine(item);
                Console.WriteLine(item);
            }
        }
        finally
        {
            if(sw != null)
            {
                sw.Dispose();
            }
        }
    }

    static void new_file()
    {
        path = @"C:\temp\list" + file + ".txt";
        if (!File.Exists(path))
        {
            using (StreamWriter sw = File.CreateText(path))
            {
            }
        }
        file++;
    }
}

      

+1


source


your alphabet is missing 0

With this in mind, your set will have 89 characters. Let's call it 100 for simplicity. The set you are looking for are 8 character length strings taken from this set. There are 100 ^ 8 of them, i.e. 10,000,000,000,000,000.

The disk space they will take up depends on how you encode them, let's be generous - let's say you are using an 8 bit char set that contains those characters and you do not enter a carriage return, so one byte per char, so what is 10,000,000,000,000,000 bytes = ~ 10 peta byes?

Do you have 10 petabytes of disk? (10,000 TB)?

[EDIT] In response to "this is not an answer":

Initial motivation - create a list? Show how big the list is. Its hard to figure out what can be done with the list if it has been updated, i.e. It will always be faster to play it than download it. Of course, any point that can be done while making the list can also be done simply by knowing its size, which above shows how to accomplish it.

There is a lot of inefficiency in your code, but if your questions are "How can I quickly create this list and write it to disk," the answer is "you literally can't."

[/ EDIT]

-2


source







All Articles