今天我测试了 Linq 和 Plinq 查询的性能影响。因此,我使用了有关msdn How to: Measure PLINQ Query Performance的文章。
void Main()
{
var source = Enumerable.Range(0, 600000000);
System.Diagnostics.Stopwatch sw;
var queryToMeasure1 = from num in source
where num % 3 == 0
select Math.Sqrt(num);
var queryToMeasure2 = from num in source.AsParallel()
where num % 3 == 0
select Math.Sqrt(num);
long freq = Stopwatch.Frequency;
Console.WriteLine("Timer frequency in ticks per second = {0}", freq);
Console.WriteLine("Measuring 1");
sw = System.Diagnostics.Stopwatch.StartNew();
foreach (var n in queryToMeasure1) { }
Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
Console.WriteLine("Measuring 2");
sw = System.Diagnostics.Stopwatch.StartNew();
foreach (var n in queryToMeasure2) { }
Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
Console.WriteLine("Measuring 3");
sw = System.Diagnostics.Stopwatch.StartNew();
System.Threading.Tasks.Parallel.ForEach(queryToMeasure1, n => {});
Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
Console.WriteLine("Measuring 4");
sw = System.Diagnostics.Stopwatch.StartNew();
System.Threading.Tasks.Parallel.ForEach(queryToMeasure2, n => {});
Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
Console.WriteLine("Measuring 5");
sw = System.Diagnostics.Stopwatch.StartNew();
queryToMeasure2.ForAll(n => {});
Console.WriteLine("Total ticks: {0} - Elapsed time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds););
}
测试环境:LinqPad4 on Win7 Enterprise, 64bit, 8GB RAM, I7-2600 (8cores)
我想通了,但无法解释为什么一个核心上的查询(测量 1)比并行查询快。我是否必须添加更多选择代表才能从并行任务中受益?
但现在结果:
1.Run:可枚举范围为60000:
Timer frequency in ticks per second = 3312851
Measuring 1
Total ticks: 3525 - Elapsed time: 1 ms
Measuring 2
Total ticks: 15802 - Elapsed time: 4 ms
Measuring 3
Total ticks: 5940 - Elapsed time: 1 ms
Measuring 4
Total ticks: 26862 - Elapsed time: 8 ms
Measuring 5
Total ticks: 4387 - Elapsed time: 1 ms
2.Run:可枚举范围为600000000:
Timer frequency in ticks per second = 3312851
Measuring 1
Total ticks: 29740243 - Elapsed time: 8977 ms
Measuring 2
Total ticks: 33722438 - Elapsed time: 10179 ms
Measuring 3
Total ticks: 77145502 - Elapsed time: 23286 ms
Measuring 4
Total ticks: 120078284 - Elapsed time: 36246 ms
Measuring 5
Total ticks: 30899585 - Elapsed time: 9327 ms
有趣的事实:在执行测试脚本之前使用垃圾收集器会大大增加测量 4 的时间:
3.运行:可枚举范围为 600000000 和垃圾收集器(来自 LinqPad):
Timer frequency in ticks per second = 3312851
Measuring 1
Total ticks: 29597830 - Elapsed time: 8934 ms
Measuring 2
Total ticks: 33532083 - Elapsed time: 10121 ms
Measuring 3
Total ticks: 76403692 - Elapsed time: 23062 ms
Measuring 4
Total ticks: 58534548 - Elapsed time: 17668 ms
Measuring 5
Total ticks: 32943622 - Elapsed time: 9944 ms
In conclusion, can I say that method 1 is the most suitable option to perform small select queries and method 5 when the select delegates will increase?