Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.

Commit 05ade82

Browse files
Adding Parallel LINQ sample
it was missing from the source
1 parent 2a6318b commit 05ade82

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

LINQ/LinqSamples/LinqSamples.sln

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EnumerableSample", "Enumera
1111
EndProject
1212
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ExpressionTreeSample", "ExpressionTreeSample\ExpressionTreeSample.xproj", "{8EB5A2E7-1A97-44B1-A6BA-8D7A5028B75A}"
1313
EndProject
14+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ParallelLinqSample", "ParallelLinqSample\ParallelLinqSample.xproj", "{1CEFFCD4-7E77-479E-970B-47D76F71794D}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{8EB5A2E7-1A97-44B1-A6BA-8D7A5028B75A}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{8EB5A2E7-1A97-44B1-A6BA-8D7A5028B75A}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{8EB5A2E7-1A97-44B1-A6BA-8D7A5028B75A}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{1CEFFCD4-7E77-479E-970B-47D76F71794D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{1CEFFCD4-7E77-479E-970B-47D76F71794D}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{1CEFFCD4-7E77-479E-970B-47D76F71794D}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{1CEFFCD4-7E77-479E-970B-47D76F71794D}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>1ceffcd4-7e77-479e-970b-47d76f71794d</ProjectGuid>
11+
<RootNamespace>ParallelLinqSample</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using static System.Console;
8+
9+
namespace ParallelLinqSample
10+
{
11+
public class Program
12+
{
13+
public static void Main()
14+
{
15+
IList<int> data = SampleData();
16+
LinqQuery(data);
17+
ExtensionMethods(data);
18+
UseAPartitioner(data);
19+
UseCancellation(data);
20+
}
21+
22+
private static void LinqQuery(IEnumerable<int> data)
23+
{
24+
WriteLine(nameof(LinqQuery));
25+
var res = (from x in data.AsParallel()
26+
where Math.Log(x) < 4
27+
select x).Average();
28+
WriteLine($"result from {nameof(LinqQuery)}: {res}");
29+
WriteLine();
30+
}
31+
32+
private static void ExtensionMethods(IEnumerable<int> data)
33+
{
34+
WriteLine(nameof(ExtensionMethods));
35+
var res = data.AsParallel()
36+
.Where(x => Math.Log(x) < 4)
37+
.Select(x => x).Average();
38+
39+
WriteLine($"result from {nameof(ExtensionMethods)}: {res}");
40+
WriteLine();
41+
}
42+
43+
private static void UseAPartitioner(IList<int> data)
44+
{
45+
WriteLine(nameof(UseAPartitioner));
46+
var res = (from x in Partitioner.Create(data, loadBalance: true).AsParallel()
47+
where Math.Log(x) < 4
48+
select x).Average();
49+
50+
WriteLine($"result from {nameof(UseAPartitioner)}: {res}");
51+
WriteLine();
52+
}
53+
54+
private static void UseCancellation(IEnumerable<int> data)
55+
{
56+
WriteLine(nameof(UseCancellation));
57+
var cts = new CancellationTokenSource();
58+
59+
Task.Run(() =>
60+
{
61+
try
62+
{
63+
var res = (from x in data.AsParallel().WithCancellation(cts.Token)
64+
where Math.Log(x) < 4
65+
select x).Average();
66+
67+
WriteLine($"query finished, sum: {res}");
68+
}
69+
catch (OperationCanceledException ex)
70+
{
71+
WriteLine(ex.Message);
72+
}
73+
});
74+
75+
WriteLine("query started");
76+
Write("cancel? ");
77+
string input = ReadLine();
78+
if (input.ToLower().Equals("y"))
79+
{
80+
cts.Cancel();
81+
}
82+
83+
WriteLine();
84+
}
85+
86+
static IList<int> SampleData()
87+
{
88+
const int arraySize = 50000000;
89+
var r = new Random();
90+
return Enumerable.Range(0, arraySize).Select(x => r.Next(140)).ToList();
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyConfiguration("")]
9+
[assembly: AssemblyCompany("")]
10+
[assembly: AssemblyProduct("ParallelLinqSample")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
// The following GUID is for the ID of the typelib if this project is exposed to COM
19+
[assembly: Guid("1ceffcd4-7e77-479e-970b-47d76f71794d")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "1.0.0-*",
3+
"buildOptions": {
4+
"emitEntryPoint": true
5+
},
6+
7+
"dependencies": {
8+
"DataLib": "1.0.0-*"
9+
},
10+
11+
"frameworks": {
12+
"netcoreapp1.0": {
13+
"imports": "dnxcore50",
14+
"dependencies": {
15+
"Microsoft.NETCore.App": {
16+
"type": "platform",
17+
"version": "1.0.0-rc2-3002702"
18+
}
19+
}
20+
},
21+
"net46": { }
22+
}
23+
}

0 commit comments

Comments
 (0)