简体   繁体   English

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

[英]Fastest way to count number of uppercase characters in c#

Any thoughts on the efficiency of this? 对此效率的任何想法? ... ...

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

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 你的:19737蜱虫

Mine: 118 ticks 我的: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. 进来大约2500蜱。 So for a "Linqy" one-liner it's pretty quick. 所以对于一个“Linqy”单行程来说它很快。

First there is no reason you need to call ToCharArray() since, assuming CommentText is a string it is already an IEnumerable<char> . 首先,有没有理由,你需要调用ToCharArray()因为,假设CommentText是一个字符串,它已经是一个IEnumerable<char> Second, you should probably be calling char.IsUpper instead of assuming you are only dealing with ASCII values. 其次,您可能应该调用char.IsUpper而不是假设您只处理ASCII值。 The code should really look like, 代码应该看起来像,

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. 一般来说,调用任何方法都会比内联代码慢,但只有在你完全确定这是代码中的瓶颈时才应该进行这种优化。

You're only counting standard ASCII and not ÃÐÊ etc. 你只计算标准的ASCII而不是ÃÊÊ等。

How about 怎么样

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

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. 现在更快,现在测试它。

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调用,如Matt建议的那样。

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: 在vshost中调试,使用字符串进行10'000次迭代:
aBcDeFGHi1287jKK6437628asghwHllmTbynerA aBcDeFGHi1287jKK6437628asghwHllmTbynerA

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM