在下面的示例中,我如何轻松将
eventScores
转换为List<int>
,以便我可以将其用作prettyPrint
方法的参数?Console.WriteLine("Example of LINQ's Where:");
List<int> scores = new List<int> { 1,2,3,4,5,6,7,8 };
var evenScores = scores.Where(i => i % 2 == 0);
Action<List<int>, string> prettyPrint = (list, title) =>
{
Console.WriteLine("*** {0} ***", title);
list.ForEach(i => Console.WriteLine(i));
};
scores.ForEach(i => Console.WriteLine(i));
prettyPrint(scores, "The Scores:");
foreach (int score in evenScores) { Console.WriteLine(score); }
i % 2 == 0
替换为i & 0x1 == 0
。现在,的确,在 x86 上实现模操作的 DIV 指令比 AND 操作要引入几个额外的 CPU 纳秒延迟。但是,JIT 的作者们知道这个事实!要使其成为性能优化,则需要知道 (1) JIT 做得不好,(2) 节省这些纳秒延迟是程序中最慢的部分,以及 (3) 程序已经不可接受地缓慢了。 - Eric Lipperti % 2 == 0
与i & 0x1 == 0
的相对效率;) - FoggyDay