松散组合比较

本文关键字:比较 组合 | 更新日期: 2023-09-27 17:54:55

比较 2 种组合的最佳方法是什么?

我的情况:

我目前有string string1 = "xxxxxx";.长度为6字符。每个字符值要么是0,要么是1,要么是x。我需要将此string与另一个具有相同字符数但值为 10string进行比较

  • 第一个string中的字符x表示第二个字符值 string可以是任何东西

  • 第一string的字符0 - 只接受第二string0

  • 第一string中的字符1 - 仅接受第二string中的1

下面是一个快速示例:

string pattern = 'xxxxxx';
string test1 = '010101';
// pass
string pattern = '1xxxxx';
string test2 = '010101';
// not pass
string pattern = '0xxxxx';
string test3 = '010101';
// pass

我为它做了一个函数:

    public bool passCombination(string pattern, string combination)
    {
        bool combination_passed = true;
        for (int i = 0; i < pattern.Length; i++)
        {
            char test_char = pattern[i];
            if (test_char != 'x' && combination[i] != test_char)
            {
                combination_passed = false;
                break;
            }
        }
        return combination_passed;
    }

这很简单。基本上我在char之后迭代char.如果它是x那么我不关心第二个字符串中的值。如果是其他字符 - 然后比较。

由于它是基于字符串的 aproach,我在考虑其他解决方案?在我的真实场景中,我必须执行大约(~700k 这样的检查 * ~150 万次(。这个非常乐观的数字:)

我正在考虑regex比较或将所有组合保存到int[]数组中并进行比较。或者也许可以使用hashes完成一些魔法?

因此,至少有3个其他选项可以提高性能。任何人都可以建议更高性能的解决方案吗?

编辑:

我确实运行了比较测试。使用旧的 aproach,我获得了 2.5 分钟的执行时间,而下面建议的新 aproach(接受的答案( - 大约 2 分钟。这大约是性能提高20%

松散组合比较

首先,在浪费时间编写过于聪明的代码以节省您可以浪费的周期之前,请确保您确实需要优化任何内容。

但是,如果您确实需要优化,您可以随时进行一些调整。它通常比循环遍历东西更快,在极少数情况下,如果不是,任何阅读你的代码的人看起来都更快

警告:如果您永远不会(或很少(多次比较任何给定的"值"字符串,则此方法没有任何优势,因为编译无论如何都涉及遍历字符串。

如果你确实有性能问题,你可以将模式"编译"成两个整数:一个是每个1都有1的模式,每个0x都有一个0;另一个是掩码,每个x都有一个0,每个01都有一个1。你每个整数浪费了 26 位,但我不会告诉任何人。

然后将值编译为 ints:1 表示 10 表示 0

编写一个具有这些模式/掩码整数的类,以及将它们与值 int 进行比较的方法。如果需要显示它们,您将"预编译"值"并将它们存储为整数而不是字符串,或者可能是具有 int 属性和字符串属性的类(或者您可以编写一个函数将这些整数转换回字符串(。

public class PatternMatcher
{
    public PatternMatcher(String pattern)
    {
        Pattern = CompilePattern(pattern);
        Mask = CompileMask(pattern);
    }
    #region Fields
    //  Could we save any cycles by making these fields instead of properties? 
    //  I think the optimizer is smarter than that. 
    public int Pattern { get; private set; }
    public int Mask { get; private set; }
    #endregion Fields
    public bool CheckValue(String value)
    {
        return CheckValue(CompileValue(value));
    }
    public bool CheckValue(int value)
    {
        //  a & b: Bitwise And
        //      Any bit that's "true" in both numbers is "true" in the result. 
        //      Any bit that's "false" in EITHER number is "false" in the result.
        //      11 & 11 == 11
        //      11 & 01 == 01
        //      11 & 10 == 10
        //      11 & 00 == 00
        //      01 & 11 == 01
        //      01 & 01 == 01
        //      01 & 10 == 00
        //      01 & 00 == 00
        //  So xx0011 -> 
        //      Pattern: 000011
        //      Mask:    001111
        //      Value    110011
        //  (110011 & 001111) == 000011
        //  (000011 & 001111) == 000011
        //
        //  000011 == 000011, so these two match. 
        return (value & Mask) == (Pattern & Mask);
    }
    public static int CompileMask(string patternString)
    {
        int mask = 0;
        int bitoffset = 0;
        //  For each character in patternString, set one bit in mask.
        //  Start with bit zero and move left one bit for each character.
        //  On x86, these bits are in reverse order to the characters in 
        //  the strings, but that doesn't matter. 
        foreach (var ch in patternString)
        {
            switch (ch)
            {
                //  If the pattern has a '0' or a '0', we'll be examining that 
                //  character in the value, so put a 1 at that spot in the mask.
                case '1':
                case '0':
                    //  a | b: Bitwise OR: If a bit is "true" in EITHER number, it's 
                    //  true in the result. So 0110 | 1000 == 1110.
                    //  a << b: Bitwise left shift: Take all the bits in a and move 
                    //  them leftward by 1 bit, so 0010 << 1 == 0100. 
                    //
                    //  So here we shift 1 to the left by some number of bits, and 
                    //  then set that bit in mask to 1. 
                    mask |= 1 << bitoffset;
                    break;
                //  If it's an 'x', we'll ignore that character in the value by 
                //  putting a 0 at that spot in the mask. 
                //  All the bits are zero already.
                case 'x':
                    break;
                default:
                    throw new ArgumentOutOfRangeException("Invalid pattern character: " + ch);
            }
            ++bitoffset;
        }
        return mask;
    }
    public static int CompilePattern(string patternString)
    {
        int pattern = 0;
        int bitoffset = 0;
        foreach (var ch in patternString)
        {
            //  For each character in patternString, set one bit in pattern.
            //  Start with bit zero and move left one bit for each character.
            switch (ch)
            {
                //  If the pattern has a 1, require 1 in the result.
                case '1':
                    pattern |= 1 << bitoffset;
                    break;
                //  For 0, require 0 in the result.
                case '0':
                    //  All the bits were zero already so don't waste time setting 
                    //  it to zero. 
                    break;
                //  Doesn't matter what we do for 'x', since it'll be masked out. 
                //  Just don't throw an exception on it. 
                case 'x':
                    break;
                default:
                    throw new ArgumentOutOfRangeException("Invalid pattern character: " + ch);
            }
            ++bitoffset;
        }
        return pattern;
    }
    public static int CompileValue(string valueString)
    {
        int value = 0;
        int bitoffset = 0;
        //  For each character in patternString, set one bit in mask.
        //  Start with bit zero and move left one bit for each character.
        foreach (var ch in valueString)
        {
            switch (ch)
            {
                //  If the value has a '1', have a 1 for that bit
                case '1':
                    value |= 1 << bitoffset;
                    break;
                //  If the value has a '0', leave a 0 for that bit
                //  All the bits were zero already.
                case '0':
                    break;
                default:
                    throw new ArgumentOutOfRangeException("Invalid pattern character: " + ch);
            }
            ++bitoffset;
        }
        return value;
    }
}

显然,如果您不能预编译您的值并将它们存储为整数(这是一个很大的"如果"(,那么您在这里浪费了时间。但是如果可以的话,您可以为每个模式创建一个,并在循环中使用它 700k+ 次。这可能比循环访问字符串 700k+ 次更快。

感觉这个问题可能缺少一些重要信息。例如,如果值存储在数据库中,则存储过程应该比加载内存中的所有数据更快。

另一方面,将数据加载到内存中可以让您更好地控制缓存(将已检查的组合保存在哈希表或数组中(和并行级别(同时检查不同处理器或机器上数据的不同部分(

此外,如果您针对大约一百万个组合检查大约一百万个模式,那么有更好的算法可以将当前复杂度从 O(n^2( 提高到更接近 O(n( http://bigocheatsheet.com/#chart

对于模式和组合的比较,您现在拥有的应该比将字符串转换为整数快一点。如果您正在比较一种形态与多种组合,那么如果您保存未x的位置并仅比较它们,则可以加快速度。例如:

foreach ( string pattern in patterns )
{
    // save the non x indexes
    var indexes = new List<int>(); 
    for (int i = 0; i < pattern.Length; i++)
        if (pattern[i] != 'x')
            indexes.Add(i);
    foreach ( string combination in combinations )
    {
        bool combination_passed = true;
        foreach (int i in indexes)
        {
            if (combination[i] != pattern[i])
            {
                combination_passed = false;
                break;
            }
        }
        // ...
    }
}