为什么.NET 4.0中的C#方法即时编译的顺序会影响它们执行的速度?例如,考虑两种等效方法:
public static void SingleLineTest()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int count = 0;
for (uint i = 0; i < 1000000000; ++i) {
count += i % 16 == 0 ? 1 : 0;
}
stopwatch.Stop();
Console.WriteLine("Single-line test --> Count: {0}, Time: {1}", count, stopwatch.ElapsedMilliseconds);
}
public static void MultiLineTest()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int count = 0;
for (uint i = 0; i < 1000000000; ++i) {
var isMultipleOf16 = i % …Run Code Online (Sandbox Code Playgroud) ILSpy表明它String.IsNullOrEmpty是以实现的方式实现的String.Length.但那么为什么String.IsNullOrEmpty(s)速度比s.Length == 0?
例如,它在此基准测试中的速度提高了5%:
var stopwatches = Enumerable.Range(0, 4).Select(_ => new Stopwatch()).ToArray();
var strings = "A,B,,C,DE,F,,G,H,,,,I,J,,K,L,MN,OP,Q,R,STU,V,W,X,Y,Z,".Split(',');
var testers = new Func<string, bool>[] { s => s == String.Empty, s => s.Length == 0, s => String.IsNullOrEmpty(s), s => s == "" };
int count = 0;
for (int i = 0; i < 10000; ++i) {
stopwatches[i % 4].Start();
for (int j = 0; j < 1000; ++j)
count += strings.Count(testers[i % …Run Code Online (Sandbox Code Playgroud) 我想知道是否有任何方法可以通过指定更深入优化的首选项来更改.NET JIT编译器的行为.如果不这样做,如果它可以进行某种配置文件引导的优化,如果它还没有,那将是很好的.