在c#中计算大写字符数的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/609704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:12:08  来源:igfitidea点击:

Fastest way to count number of uppercase characters in c#

c#linq

提问by peterorum

Any thoughts on the efficiency of this? ...

关于这个效率的任何想法?...

CommentText.ToCharArray().Where(c => c >= 'A' && c <= 'Z').Count()

采纳答案by Matt Hamilton

Ok, just knocked up some code to time your method against this:

好的,只是敲了一些代码来为你的方法计时:

int count = 0;
for (int i = 0; i < s.Length; i++)
{
    if (char.IsUpper(s[i])) count++;
}

The result:

结果:

Yours: 19737 ticks

Mine: 118 ticks

您的:19737 个滴答声

我的:118 滴答

Pretty big difference! Sometimes the most straight-forward way is the most efficient.

相当大的不同!有时,最直接的方法是最有效的。

Edit

编辑

Just out of interest, this:

只是出于兴趣,这个:

int count = s.Count(c => char.IsUpper(c));

Comes in at at around 2500 ticks. So for a "Linqy" one-liner it's pretty quick.

在大约 2500 个滴答声中出现。所以对于“Linqy”单线来说,它非常快。

回答by Dead account

You're only counting standard ASCII and not ?Dê etc.

你只计算标准 ASCII 而不是 ?Dê 等。

How about

怎么样

CommentText.ToCharArray().Where(c => Char.IsUpper(c)).Count()

回答by Binary Worrier

Without even testing I'd say

我什至没有测试我会说

int count = 0;
foreach (char c in commentText)
{
    if (Char.IsUpper(c))
        count++;
}

is faster, off now to test it.

更快,现在关闭来测试它。

回答by chuckj

First there is no reason you need to call ToCharArray()since, assuming CommentTextis a string it is already an IEnumerable<char>. Second, you should probably be calling char.IsUpperinstead of assuming you are only dealing with ASCII values. The code should really look like,

首先,您没有理由需要调用,ToCharArray()因为假设CommentText是一个字符串,它已经是一个IEnumerable<char>. 其次,您可能应该调用char.IsUpper而不是假设您只处理 ASCII 值。代码应该看起来像,

CommentText.Count(char.IsUpper)

Third, if you are worried about speed there isn't much that can beat the old for loop,

第三,如果你担心速度,没有什么可以打败旧的 for 循环,

int count = 0;
for (int i = 0; i < CommentText.Length; i++) 
   if (char.IsUpper(CommentText[i]) count++;

In general, calling any method is going to be slower than inlining the code but this kind of optimization should only be done if you are absolutely sure this is the bottle-neck in your code.

一般来说,调用任何方法都会比内联代码慢,但只有在您绝对确定这是代码中的瓶颈时才应该进行这种优化。

回答by Guffa

What you are doing with that code is to create a collection with the characters, then create a new collection containing only the uppercase characters, then loop through that collection only to find out how many there are.

您使用该代码所做的是创建一个包含字符的集合,然后创建一个仅包含大写字符的新集合,然后遍历该集合以找出有多少个字符。

This will perform better (but still not quite as good as a plain loop), as it doesn't create the intermediate collections:

这会表现得更好(但仍然不如普通循环好),因为它不会创建中间集合:

CommentText.Count(c => Char.IsUpper(c))

Edit: Removed the ToCharArray call also, as Matt suggested.

编辑:也删除了 ToCharArray 调用,正如马特建议的那样。

回答by flq

I've got this

我有这个

Regex x = new Regex("[A-Z]{1}", 
  RegexOptions.Compiled | RegexOptions.CultureInvariant);
int c = x.Matches(s).Count;

but I don't know if its particularly quick. It won't get special chars either, I s'pose

但我不知道它是否特别快。它也不会得到特殊字符,我想

EDIT:

编辑:

Quick comparison to this question's answer. Debug in vshost, 10'000 iterations with the string:
aBcDeFGHi1287jKK6437628asghwHllmTbynerA

快速比较这个问题的答案。在 vshost 中调试,使用字符串进行 10'000 次迭代:
aBcDeFGHi1287jKK6437628asghwHllmTbynerA

  • The answer: 20-30 ms
  • The regex solution: 140-170 ms
  • 答案:20-30 毫秒
  • 正则表达式解决方案:140-170 毫秒