English 中文(简体)
是否更快地比较了雷克与伊格诺雷塞或图洛尔的扼杀方法?
原标题:Is it faster to compare strings with Regex with IgnoreCase or with ToLower method of string?
  • 时间:2012-05-13 16:00:31
  •  标签:
  • c#
  • regex

鉴于这些情况:

string s1 = "Abc";
string s2 = "ABC";

更快:

Regex.Match(s1, s2, RegexOptions.Ign或eCase)

s1.ToLower() == s2.ToLower()

If they are the same 或 the one is faster then the other, so when its better to use one over the other?

最佳回答

也许第二步比较快,但我避免这两种做法。

更好的方法是使用string.Equals 。 缩略语 论点:

s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase)

见网站:

问题回答

Theoretically speaking, comparing 2 strings should be faster, RegEx are know to be rather slow.

However, if you want to match a string s1 to a RegEx s2 while ignoring case (This is not the same as comparing 2 strings), then the first solution is better as it should avoid creating another string.

与这种问题总是一样,我将有一个基准,对两个业绩进行比较:

@Mark Byers已经张贴了正确的答案。

我想强调,您应为never使用Tolower进行细微比较。 这是不正确的。

s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase) //#1
s1.ToLower() == s2.ToLower() //#2
s1.ToLowerInvariant() == s2.ToLowerInvariant() //#3

(2) and (3) are both incorrect when it comes to exotic languages and strange characters. The Turkish "I" is the classical example.

www.un.org/spanish/ecosoc 航道使用第1号,甚至在哈希姆。

(非常特殊的情况除外)

应当指出,<代码>Regex.Match(s1, s2, RegexOptions. IgnoreCase)不是在一般情况下检查对案件敏感的平等的一种安全方法。 审议s2 is *>的.Regex.Match将始终如一地交回s!

This may be the most extreme case of premature optimization I ve ever seen. Trust me, you will never run into a situation where this issue will be relevant.

而不要听从所有那些告诉你们的人,不要因为“the慢”而避开reg。 Badly written regexes can actual hog resources about awful, but that s thefall of whoever written the regex. 合理精心设计的规章,对于绝大多数任务的人来说,是足够快的。

这里对拟议的3种方法进行了小的比较:

Regex: 282ms ToLower: 67ms Equals: 34ms

public static void RunSnippet()
{
    string s1 = "Abc";
    string s2 = "ABC";

    // Preload
    compareUsingRegex(s1, s2);
    compareUsingToLower(s1, s2);
    compareUsingEquals(s1, s2);

    // Regex
    Stopwatch swRegex = Stopwatch.StartNew();
    for (int i = 0; i < 300000; i++) 
        compareUsingRegex(s1, s2);
    Console.WriteLine(string.Format("Regex: {0} ms", swRegex.ElapsedMilliseconds));

    // ToLower
    Stopwatch swToLower = Stopwatch.StartNew();
    for (int i = 0; i < 300000; i++) 
        compareUsingToLower(s1, s2);
    Console.WriteLine(string.Format("ToLower: {0} ms", swToLower.ElapsedMilliseconds));

    // ToLower
    Stopwatch swEquals = Stopwatch.StartNew();
    for (int i = 0; i < 300000; i++) 
        compareUsingEquals(s1, s2);
    Console.WriteLine(string.Format("Equals: {0} ms", swEquals.ElapsedMilliseconds));
}

private static bool compareUsingRegex(string s1, string s2) 
{
    return Regex.IsMatch(s1, s2, RegexOptions.IgnoreCase);
}

private static bool compareUsingToLower(string s1, string s2) 
{
    return s1.ToLower() == s2.ToLower();
}

private static bool compareUsingEquals(string s1, string s2) 
{
    return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
}

比较速度较快,但不能转换为低级或高级案件,然后比较,最好采用平等比较办法,使情况敏感。 例如:

        s1.Equals(s2, StringComparison.OrdinalIgnoreCase)




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签