嵌套For循环加速性能

本文关键字:性能 加速 循环 For 嵌套 | 更新日期: 2023-09-27 18:12:55

我有一个字符串列表列表,以便将字符串与'1'的数量分组,如string =" 00000"属于第一组,string =" 00001"属于第二组。所有字符串长度相等。现在我比较第一组和第二组,第二组和第三组,很快……就像图片里那样。将第一组中的第一个元素与第二组中的所有元素进行比较。直到比较每个字符串。是否有一种方法可以加速我的程序的性能?所以我可以实现32000字符串15长度。

编辑

抱歉过去的帖子。看完之后,我意识到我这样问太蠢了。该程序的目标是简化程序。基于 Quine-McCluskey算法的

考虑
000
001
010
011
100
101
110
111

我将它们按1的个数分组

000
001
010
100
011
101
110
111

然后比较该组中的每个字符串与下一组

group 1
000
group 2
001
010
100
group 3
011
101
110
group1 -> group2
------------------
    000 -> 001 = 00-
    000 -> 010 = 0-0
    000 -> 100 = -00
------------------
group2 ->group3
--------------------  
    001 -> 011 = 0-1
    001 -> 101 = -01
    001 -> 110 = no output
    010 -> 011 = 01-
    010 -> 101 = no output
    010 -> 110 = -10
    100 -> 011 = no output
    100 -> 101 = 10-
    100 -> 110 = 1-0
---------------------
etc.

然后再将输出按数字1分组,并再次比较它们,直到没有字符串可以比较。

我需要实现一个15变量,但它需要永远为程序完成。知道怎么加快速度吗。我正在测试线程,但只有一点改进。

字符串数:2048变量长度:11时间:10分钟

需要实现

字符串数:32767变量长度:15时间:无法实现

 List<List<string>> ImplicantsByOneFinal = new List<List<string>>();
 List<List<string>> TermsByOne = new List<List<string>>();

是否有一种方法或算法来改进这段代码。当有11到15个变量时,它会变慢。

bool CombineAndGroup(List<List<string>> ImplicantsByOne)
{ 
            TermsByOne = new List<List<string>>();
            int combined = 0; 
            for (int i = 0; i < ImplicantsByOne.Count - 1; i++)
            { 
                List<string> termsGrouped = new List<string>();
                for (int j = 0; j < ImplicantsByOne[i].Count; j++)
                { 
                    int combination = 0;
                    int num1 = Convert.ToInt32((ImplicantsByOne[i][j]).Replace('-','0'), 2); 
                        for (int k = 0; k < ImplicantsByOne[i + 1].Count; k++)
                        { 
                            int num2 = Convert.ToInt32((ImplicantsByOne[i + 1][k]).Replace('-', '0'), 2);
                            int num3 = num2 - num1;
                            double num4 = Math.Log((double)num3, (double)2); 
                            if (((num4 % 1) == 0) && (num3 > 0) && (Esum(ImplicantsByOne[i][j]) == Esum(ImplicantsByOne[i + 1][k])))
                            {  
                                string combinedMinterm = CompareString(ImplicantsByOne[i][j], ImplicantsByOne[i + 1][k]); 
                                if (!termsGrouped.Contains(combinedMinterm))
                                {
                                    termsGrouped.Add(combinedMinterm); 
                                }  
                            }
                        }   
                }
                if (termsGrouped.Count > 0)
                {
                    combined += termsGrouped.Count;
                } 
                TermsByOne.Add(termsGrouped);
            }
            return (combined > 0) ? true : false;
        } 
 private int Esum(String binCode)
        {
            binCode = binCode.Replace('1','0');
            binCode = binCode.Replace('-', '1');
            int esum = Convert.ToInt32(binCode, 2);
            return esum;
        }
//Purpose of CompareString is to compare two string and change the unique char to '-'
//like 000 and 001 = 00-
  private string CompareString(string str1, string str2)
        { 
            if (str1 == str2)
            { 
                CountCompareStringLoops++;
                return str1;
            }
            else 
            { 
                if (str1.Length == 1)
                { 
                    return "-";
                }
                int halflength = str1.Length / 2; 
                return CompareString(str1.Substring(0, halflength), str2.Substring(0, halflength)) + CompareString(str1.Substring(halflength), str2.Substring(halflength)); 
            }
        }

主程序

 MintermsByOne = Loaded with string 000 001 and so on
CombineAndGroup(MintermsByOne);
 ImplicantsByOneFinal = TermsByOne; 
 while (CombineAndGroup(TermsByOne))
 {
        ImplicantsByOneFinal = TermsByOne; 
 }

Output ImplicantsByOneFinal

嵌套For循环加速性能

说实话,你真的不清楚自己想要达到什么目标……你的描述与你的代码不符。(例如,您的代码从不提到字符'1'。您从未使用调用CompareString的结果的事实也是可疑的。)LINQ应该使实现你的描述"group the string with number of '1' "变得简单和有效:

var grouped = strings.GroupBy(x => x.Count(c => c == '1'));

将只计算每个字符串中'1'字符的数量一次。您不需要将任何字符串与另一个字符串进行比较。

如果这不是你真正想要做的,你需要澄清你真正的目标是什么。

我不知道如何写c#,但我想帮忙。我的代码是用Java写的

1. 我认为==是一个O(n)操作,你的CompareString可能是O(nlgn),其中n = str1.Length。使用更简单和更快的O(n)方法,看看时间是否减少:

private String CompareString(String str1, String str2) {
    StringBuilder sb = new StringBuilder(str1.length());
    for (int i = 0; i < str1.length(); i++) {
        if (str1.charAt(i) == str2.charAt(i))
            sb.append(str1.charAt(i));
        else
            sb.append('-');
    }
    return sb.toString();
}

2。嗯,我发现有很多ToInt32。一次性计算ImplicantsByOne中所有字符串的结果,然后再使用它。Esum也是。

3.检查num3是否为2的幂:

private boolean isPowerOfTwo(int x) {
    return (x > 0 && (x & (x - 1)) == 0);
}