为什么ToUpperInvariant()比ToLowerInvariant()更快? [英] Why is ToUpperInvariant() faster than ToLowerInvariant()?
问题描述
我通过Jeffrey Richter通过C#在CLR中读到, String.ToUpperInvariant()
比 String.ToLowerInvariant()$ c $更快c>。他说这是因为FCL使用ToUpperInvariant对字符串进行规范化,因此该方法是超优化的。在我的计算机上运行了几个快速测试,我同意
ToUpperInvariant()
的确确实要快一些。
I read in CLR via C# by Jeffrey Richter that String.ToUpperInvariant()
is faster than String.ToLowerInvariant()
. He says that this is because the FCL uses ToUpperInvariant to normalise strings, so the method is ultra-optimised. Running a couple quick tests on my machine, I concur that ToUpperInvariant()
is indeed slightly faster.
我的问题是是否有人知道该功能实际上是如何在技术水平上进行优化的,和/或为何对 ToLowerInvariant()
也未应用相同的优化。
My question is if anybody knows how the function is actually optimised on a technical level, and/or why the same optimisations were not applied to ToLowerInvariant()
as well.
关于重复问题:提出的重复问题确实无法回答我的问题。我了解使用 ToUpperInvariant
代替 ToLowerInvariant
的好处,但我想知道的是为什么/为什么 ToUpperInvariant
的效果更好。
Concerning the "duplicate": The proposed "duplicate" question really doesn't provide an answer to my question. I understand the benefits of using ToUpperInvariant
instead of ToLowerInvariant
, but what I would like to know is how/why ToUpperInvariant
performs better. This point is not addressed in the "duplicate".
推荐答案
因为现在更容易阅读实现 InternalChangeCaseString
,我们可以看到它主要调用Win32函数 LCMapStringEx
。关于$$ c的 LCMAP_UPPERCASE
与 LCMAP_LOWERCASE
的传递性能,似乎没有任何注释或讨论。 $ c> dwMapFlags 参数。调用 InternalChangeCaseString
使用标志 isToUpper
,如果 true
,则该标志可能会会导致编译器(或JITter)进行更好的优化,但是由于对 LCMapStringEx
的调用必须设置ap / invoke调用框架,并且调用本身必须起作用,所以我我不确定在那里会节省很多时间。
Since it is now easier to read the CLR source which implements InternalChangeCaseString
, we can see that it mostly calls down to the Win32 function LCMapStringEx
. There appears to be no notes or any discussion on the performance of passing in LCMAP_UPPERCASE
vs. LCMAP_LOWERCASE
for the dwMapFlags
parameter. Calling InternalChangeCaseString
uses a flag isToUpper
which, if true
may result in better optimization by the compiler (or JITter), but since the call to LCMapStringEx
has to setup a p/invoke call frame and the call itself has to do work, I'm not sure a lot of time is saved there.
也许该建议是其他实现的保留,但是我看不到任何能以一种方式或其他方式显着提高速度优势的东西。
Perhaps the recommendation is a hold over from some other implementation, but I can't see anything that would provide a significant speed advantage one way or the other.
这篇关于为什么ToUpperInvariant()比ToLowerInvariant()更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!