String comparison in dotnet framework 4

后端 未结 3 1160
情书的邮戳
情书的邮戳 2021-01-18 15:03

I will explain my problem(excuse my bad English), I have a .NET exe in which every milliseconds of processing is very important.

This program does lots of string co

相关标签:
3条回答
  • 2021-01-18 15:20

    This is a known issue in .NET 4.

    Here is the MS Connect report.

    0 讨论(0)
  • 2021-01-18 15:28

    Ok i have a response of one of my question.

    With reflector i can see the difference between framework 2 and 4 and that explain my perforamnce issue.

        public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType)
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }
        if ((startIndex < 0) || (startIndex > this.Length))
        {
            throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
        }
        if ((count < 0) || (startIndex > (this.Length - count)))
        {
            throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
        }
        switch (comparisonType)
        {
            case StringComparison.CurrentCulture:
                return CultureInfo.CurrentCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.None);
    
            case StringComparison.CurrentCultureIgnoreCase:
                return CultureInfo.CurrentCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.IgnoreCase);
    
            case StringComparison.InvariantCulture:
                return CultureInfo.InvariantCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.None);
    
            case StringComparison.InvariantCultureIgnoreCase:
                return CultureInfo.InvariantCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.IgnoreCase);
    
            case StringComparison.Ordinal:
                return CultureInfo.InvariantCulture.CompareInfo.IndexOf(this, value, startIndex, count, CompareOptions.Ordinal);
    
            case StringComparison.OrdinalIgnoreCase:
                return TextInfo.IndexOfStringOrdinalIgnoreCase(this, value, startIndex, count);
        }
        throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), "comparisonType");
    }
    

    This is the base code of function IndexOf of the 2 framework (no difference between 4 and 2)

    But in the function TextInfo.IndexOfStringOrdinalIgnoreCase there are differences :

    Framework 2 :

        internal static unsafe int IndexOfStringOrdinalIgnoreCase(string source, string value, int startIndex, int count)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }
        return nativeIndexOfStringOrdinalIgnoreCase(InvariantNativeTextInfo, source, value, startIndex, count);
    }
    

    Framework 4 :

        internal static int IndexOfStringOrdinalIgnoreCase(string source, string value, int startIndex, int count)
    {
        if ((source.Length == 0) && (value.Length == 0))
        {
            return 0;
        }
        int num = startIndex + count;
        int num2 = num - value.Length;
        while (startIndex <= num2)
        {
            if (CompareOrdinalIgnoreCaseEx(source, startIndex, value, 0, value.Length, value.Length) == 0)
            {
                return startIndex;
            }
            startIndex++;
        }
        return -1;
    }
    

    The main algorithm has changed in framework 2 the call is a nativeDll that has been removed of framework 4. Its good to know

    0 讨论(0)
  • 2021-01-18 15:39

    I cannot answer on your specific .NET 4 speed issue.

    However you'd probably gain much more speed by improving your algo. Check out the Rabin-Karp string search algo.

    0 讨论(0)
提交回复
热议问题