C# 有可能让它跑得更快吗?

C# 有可能让它跑得更快吗?,c#,performance,parallel-processing,C#,Performance,Parallel Processing,我已经尝试了所有的方法,为cycle添加了一个特殊的并行程序,但它还不够快。 有没有办法让它跑得更快? 我知道在GPU上进行这些计算也是可能的,但我没有这方面的经验 作为比较,如果它正在使用其全部潜力或不是pc规格,请点击此处: CPU:AMD Ryzen 5 3400G,带Radeon Vega Graphics 3.70 GHz GPU:AMD Radeon rx580 8GB 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系

我已经尝试了所有的方法,为cycle添加了一个特殊的并行程序,但它还不够快。 有没有办法让它跑得更快? 我知道在GPU上进行这些计算也是可能的,但我没有这方面的经验

作为比较,如果它正在使用其全部潜力或不是pc规格,请点击此处:

CPU:AMD Ryzen 5 3400G,带Radeon Vega Graphics 3.70 GHz

GPU:AMD Radeon rx580 8GB

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统计时器;
名称空间MyProgram
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
} 
int[]代码;
整数时间=0;
字符[]个字符=新字符[]
{
‘a’、‘b’、‘c’、‘d’、‘e’、‘f’、‘g’、‘h’、‘i’、‘j’、‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’,
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
};
字符串passTry=“”;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
代码=空;
时间=0;
passTry=“”;
code=newint[int.Parse(LengthBox.Text)];
AppendText(“试图破解密码…”);
AppendText(Environment.NewLine);
Parallel.For(0,int.Parse(LengthBox.Text))i=>
{
代码[i]=0;
});
timer1.Enabled=true;
Task.Run(()=>
{
while(passTry!=passOrig.Text)
{
Parallel.For(0,(code.Length-1),o=>
{
如果(代码[o]>34&&o
{
if(passTry.Length
我的代码采用了一个简单的组合锁暴力算法。从0开始。 然后向第一个数字添加一个,当该数字达到最大值时,将重置该数字并将其添加到下一个数字,然后在下一个数字达到最大值时,将其添加到下一个数字,直到与字符串连接的转换字符的结果与字符串passOrig.Text不匹配

Combination lock+ conversion
    
    0 0 0 0 = A A A A
    1 0 0 0 = B A A A
    2 0 0 0 = C A A A 
    3 0 0 0 = D A A A
    0 1 0 0 = A B A A
    1 1 0 0 = B B A A
    2 1 0 0 = C B A A
    3 1 0 0 = D B A A
    0 2 0 0 = A C A A


//code to string conversion

Parallel.For(0, (code.Length), i =>
                            {
                                if (passTry.Length < code.Length)
                                {
                                    passTry +=characters[code[i]];
                                }
                            });
组合锁+转换
0 0=A
1 0 0=B A A
2 0 0=C A A A
3 0 0=D A A A
0 1 0 0=A B A A
1010=B A
2 1 0 0=C B A A
3 1 0 0=D B A A
0 2 0 0=A C A A
//代码到字符串的转换
Parallel.For(0,(code.Length),i=>
{
if(passTry.Length

编辑:这只是部署后的第一部分,原始密码将不为人所知。唯一已知的值是密码与否(真或假)thx对于所有答案,有2个有趣的答案,但我认为我目前没有足够的大脑能力去理解它们。

我试着不去想它,写了这个单线程版本,在我的笔记本电脑上,它似乎比问题中显示的快了数百倍

我使用“zzzz”进行测试,因为这是我实现中最糟糕的情况

我的版本需要~0.1秒

您的版本使用的时间>10秒

static void Naive(string password)
{
    var sw = Stopwatch.StartNew();
    var characters = new string(new Char[]
    {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
    });
    int characterLength = characters.Length;
    
    // Let's make the password an array of numbers
    byte[] passwordAsNumbers = password.Select(c =>(byte)characters.IndexOf(c)).ToArray();

    var attempt = new byte[password.Length];

    Find(0);
    var found = new string(attempt.Select(i => characters[i]).ToArray());
    var elapsed = sw.Elapsed;

    bool Find(int index)
    {
        if (index >= attempt.Length) return false;

        for (byte i = 0; i < characterLength; i++)
        {
            attempt[index] = i;
            if (attempt.SequenceEqual(passwordAsNumbers)) return true;
            if (Find(index + 1)) return true;
        }
        return false;
    }
}
static void Naive(字符串密码)
{
var sw=Stopwatch.StartNew();
变量字符=新字符串(新字符[]
{
“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”、“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”
});
int characterLength=字符。长度;
//让我们将密码设置为一个数字数组
byte[]passwordAsNumbers=password.Select(c=>(byte)characters.IndexOf(c)).ToArray();
var trunt=新字节[password.Length];
发现(0);
var found=新字符串(尝试.选择(i=>字符
static void Naive(string password)
{
    var sw = Stopwatch.StartNew();
    var characters = new string(new Char[]
    {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
    });
    int characterLength = characters.Length;
    
    // Let's make the password an array of numbers
    byte[] passwordAsNumbers = password.Select(c =>(byte)characters.IndexOf(c)).ToArray();

    var attempt = new byte[password.Length];

    Find(0);
    var found = new string(attempt.Select(i => characters[i]).ToArray());
    var elapsed = sw.Elapsed;

    bool Find(int index)
    {
        if (index >= attempt.Length) return false;

        for (byte i = 0; i < characterLength; i++)
        {
            attempt[index] = i;
            if (attempt.SequenceEqual(passwordAsNumbers)) return true;
            if (Find(index + 1)) return true;
        }
        return false;
    }
}
public bool IsItTheCombination(string combo) =>
  return combo=="FEDC"; //returns true if the input is FEDC
for(int x = 0; x <= 9999; x++){

}
for(int x = 0; x <= 9999; x++){

  int thousands = x/1000;
  int hundreds = (x/100)%10;
  int tens = (x/10)%10;
  int units = x%10;

}
charc c = (char)('A' + 0);
for(int x = 0; x <= 9999; x++){

  char thousands = (char)('A' + x/1000);
  char hundreds = (char)('A' + (x/100)%10);
  char tens = (char)('A' + (x/10)%10);
  char units = (char)('A' + x%10);

}
char[] guess = new char[4];
for(int x = 0; x <= 9999; x++){

  guess[0] = (char)('A' + x/1000);
  guess[1] = (char)('A' + (x/100)%10);
  guess[2] = (char)('A' + (x/10)%10);
  guess[3] = (char)('A' + x%10);

}
char[] guess = new char[4];
for(int x = 0; x <= 9999; x++){

  guess[0] = (char)('A' + x/1000);
  guess[1] = (char)('A' + (x/100)%10);
  guess[2] = (char)('A' + (x/10)%10);
  guess[3] = (char)('A' + x%10);

  var guessStr = new string(guess);
  if(IsItTheCombination(guessStr)){
    Console.WriteLine("The combo is " + guessStr);
  }
}
  guess[3] = (char)('A' + x%10);
  x=x/10;
  guess[2] = (char)('A' + x%10);
  x=x/10;
  guess[1] = (char)('A' + x%10);
  x=x/10;
  guess[0] = (char)('A' + x%10);
char[] guess = new char[20];
for(int x = (int)Math.Pow(10, guess.Length-1) - 1; x >= 0; x--){

  int num = x;                                   //take a copy of x because we'll modify it with division and we don't want to lose our place in the brute forcing

  
  guess[guess.Length - 1] = (char)('A' + x%10);  //do the right most digit as a special case (no division)
  for(int c = guess.Length - 2; c >= 0; c--){
    num = num/10;                                //cut a digit off
    guess[c] = (char)('A' + num%10);             //what is the rightmost digit?
  }

  var guessStr = new string(guess);
  if(IsItTheCombination(guessStr)){
    Console.WriteLine("The combo is " + guessStr);
  }
}