在C#中生成多个唯一字符串

时间:2013-08-06 18:49:04

标签: c# string unique

对于我的项目,我必须生成一个唯一字符串列表。 一切都很好,但我的问题是它最后很慢。 我尝试过使用Parallel-Loops,但我发现我使用的ConcurrentBag<T>也很慢。 现在我正在使用一个简单的for循环和List<T>,它现在有点快,但也很慢。

这是我的代码:

private List<string> Generate(int start, int end, bool allowDupes)
    {
        var list = new List<string>();
        var generator = new StringGenerator(LowerCase, UpperCase, Digits, NumberOfCharacters);
        for (var i = start; i < end; i++)
        {
            StringBuilder sb;
            while (true)
            {
                sb = new StringBuilder();
                for (var j = 0; j < NumberOfSegments; j++)
                {
                    sb.Append(generator.GenerateRandomString());
                    if (j < NumberOfSegments - 1)
                    {
                        sb.Append(Delimiter);
                    }
                }
                if (!allowDupes)
                {
                    if (list.Contains(sb.ToString()))
                    {
                        continue;
                    }
                }
                break;
            }
            list.Add(sb.ToString());
            GeneratedStringCount = i + 1;
        }
        return new List<string>(list);
    }

我也和老师谈过,他会使用相同的算法生成这些字符串。 你知道更好的解决方案吗? (GenerateRandomString()中的StringGenerator方法很简单,并且不会消耗太多性能。list.Contains(xy)占用了大量资源。[Visual Studio中的性能分析])

2 个答案:

答案 0 :(得分:2)

List.Contains很慢。请改用HashSet

private List<string> Generate(int start, int end, bool allowDupes)
{
    var strings = new HashSet<string>();
    var list = new List<string>();
    var generator = new StringGenerator(LowerCase, UpperCase, Digits, NumberOfCharacters);
    for (var i = start; i < end; i++)
    {
        while (true)
        {
            string randomString = GetRandomString();
            if (allowDupes || strings.Add(randomString))
            {
                list.Add(randomString);
                break;
            }
        }
        GeneratedStringCount = i + 1;
    }
    return new List<string>(list);
}

private string GetRandomString()
{
    var segments = Enumerable.Range(1, NumberOfSegments)
        .Select(_ => generator.GenerateRandomString());
    var result = string.Join(Delimeter, segments);
    return result;
}

这仍然有可能导致性能下降,但您可以通过智能GenerateRandomString功能来解决这个问题。

答案 1 :(得分:0)

public static String GenerateEightCode( int codeLenght, Boolean isCaseSensitive)
    {
        char[] chars = GetCharsForCode(isCaseSensitive);
        byte[] data = new byte[1];
        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);
        data = new byte[codeLenght];
        crypto.GetNonZeroBytes(data);
        StringBuilder sb = new StringBuilder(codeLenght);
        foreach (byte b in data)
        {
            sb.Append(chars[b % (chars.Length)]);
        }

        string key = sb.ToString();

        if (codeLenght == 8)
            key = key.Substring(0, 4) + "-" + key.Substring(4, 4);
        else if (codeLenght == 16)
            key = key.Substring(0, 4) + "-" + key.Substring(4, 4) + "-" + key.Substring(8, 4) + "-" + key.Substring(12, 4);

        return key.ToString();
    }

    private static char[] GetCharsForCode(Boolean isCaseSensitive)
    {
        // all - abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
        char[] chars = new char[58];
        if (isCaseSensitive)
        {
            chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789".ToCharArray();//počet unikátních kombinací 4 - 424 270, 8 - 1 916 797 311, 16 - 7.99601828013E+13
        }
        else
        {
            chars = new char[35];
            chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".ToCharArray();//počet unikátních kombinací 4 - 52 360, 8 - 23 535 820, 16 - 4 059 928 950
        }

        return chars;
    }