含有比StartsWith快? [英] Contains is faster than StartsWith?

查看:231
本文介绍了含有比StartsWith快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个顾问来了昨天,不知怎么串的话题上来。他提到,他注意到,对于字符串小于一定的长度,包含实际上是快于 StartsWith 。我不得不看到它与我自己的两只眼睛,所以我写了一个小程序,果然,包含快!

A consultant came by yesterday and somehow the topic of strings came up. He mentioned that he had noticed that for strings less than a certain length, Contains is actually faster than StartsWith. I had to see it with my own two eyes, so I wrote a little app and sure enough, Contains is faster!

这怎么可能?

DateTime start = DateTime.MinValue;
DateTime end = DateTime.MinValue;
string str = "Hello there";

start = DateTime.Now;
for (int i = 0; i < 10000000; i++)
{
    str.Contains("H");
}
end = DateTime.Now;
Console.WriteLine("{0}ms using Contains", end.Subtract(start).Milliseconds);

start = DateTime.Now;
for (int i = 0; i < 10000000; i++)
{
    str.StartsWith("H");
}
end = DateTime.Now;
Console.WriteLine("{0}ms using StartsWith", end.Subtract(start).Milliseconds);

输出:

726ms using Contains 
865ms using StartsWith

我试着用长字符串呢!

I've tried it with longer strings too!

推荐答案

尝试使用秒表测量速度,而不是的DateTime 检查。

Try using StopWatch to measure the speed instead of DateTime checking.

<一个href="http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events">http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events

我觉得关键是下面的重要组成部分粗体:

I think the key is the following the important parts bolded:

包含

此方法执行   (大小写敏感的,   文化不敏感)进行比较。

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

StartsWith

此方法执行   (区分大小写和文化敏感)   比较使用当前的文化。

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

我觉得关键是在序号比较金额为:

I think the key is the ordinal comparison which amounts to:

这是序号排序比较基础的弦   每个字符的数值   对象字符串中。一个序   比较是自动   区分大小写因为小写   和大写字符版本   有不同的code点。然而,   如果情况是不重要的   应用程序,你可以指定一个   序号比较忽略的情况下。   这等同于转换   字符串使用为大写   不变的文化,然后进行   这一结果序号比较。

An ordinal sort compares strings based on the numeric value of each Char object in the string. An ordinal comparison is automatically case-sensitive because the lowercase and uppercase versions of a character have different code points. However, if case is not important in your application, you can specify an ordinal comparison that ignores case. This is equivalent to converting the string to uppercase using the invariant culture and then performing an ordinal comparison on the result.

参考文献:

<一个href="http://msdn.microsoft.com/en-us/library/system.string.aspx">http://msdn.microsoft.com/en-us/library/system.string.aspx

<一个href="http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx">http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

<一个href="http://msdn.microsoft.com/en-us/library/baketfxw.aspx">http://msdn.microsoft.com/en-us/library/baketfxw.aspx

使用反射器,你可以看到code两个:

Using Reflector you can see the code for the two:

public bool Contains(string value)
{
    return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}

public bool StartsWith(string value, bool ignoreCase, CultureInfo culture)
{
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }
    if (this == value)
    {
        return true;
    }
    CultureInfo info = (culture == null) ? CultureInfo.CurrentCulture : culture;
    return info.CompareInfo.IsPrefix(this, value,
        ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None);

}

这篇关于含有比StartsWith快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆