浅谈分组统计
2009-06-14 08:33:35 来源:WEB开发网要注意 Linq 方法无论是运行时间还是占用的内存都更大。
我们来看看 Main 方法:
static void Main(string[] args)
{
try
{
new Program().Run(Console.Out, int.Parse(args[0]));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
void Run(TextWriter writer, int n)
{
var tuples = GetTuples(n * 1024 * 1024);
Write("ForEach", writer, ForEach(tuples));
Write("Iterate", writer, Iterate(tuples));
Write(" Linq ", writer, Linq(tuples));
}
其中的 Write 方法如下所示:
void Write(string title, TextWriter writer, IEnumerable<Tuple<int, int, double>> tuples)
{
writer.WriteLine("==========> " + title + " <============");
writer.WriteLine("Key ------Count Average----------");
var count = 0;
var sum = 0.0;
foreach (var t in tuples)
{
writer.WriteLine("{0,3} {1,11:N0} {2}", t.Item1, t.Item2, t.Item3);
count += t.Item2;
sum += t.Item2 * t.Item3;
}
writer.WriteLine("--- ----------- -----------------");
writer.WriteLine("{0,3} {1,11:N0} {2}", tuples.Count(), count, sum / count);
writer.WriteLine();
}
更多精彩
赞助商链接