Csharp: Parallel LINQ

https://devtut.github.io/csharp/parallel-linq-plinq.html#simple-example
https://github.com/PacktPublishing/Hands-On-Parallel-Programming-with-C-8-and-.NET-Core-3.0
https://gist.github.com/gfoidl/d8250ff11a5c70972cb8164622792ba1
https://github.com/lassevk/ObjectDumper
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/order-preservation-in-plinq
LINQPad
https://github.com/giawa/RedPandaOS
http://www.albahari.com/nutshell/E9-CH22.aspx
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10
https://github.com/WolfgangOfner/CSharp-9.0/tree/master/CSharp9
https://github.com/apress/beg-cpp
https://github.com/Apress/beginning-cpp20
https://github.com/packtpublishing/beginning-cpp-programming

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Linq.Expressions;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
 
 
//https://devtut.github.io/csharp/parallel-linq-plinq.html#simple-example
/// <summary>
/// geovindu 涂聚文 Geovin Du
/// </summary>
public partial class _Default : Page
{
 
 
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        var sequence = Enumerable.Range(1, 10000).Select(x => -1 * x); // -1, -2, ...
        var evenNumbers = sequence.AsParallel()
                                  .OrderBy(x => x)
                                  .Take(5000)
                                  .AsUnordered()
                                  .Where(x => x % 2 == 0) // This line won't be affected by ordering
                                  .ToList();
 
 
        string wordLookupFile = Path.Combine(Path.GetTempPath(), "WordLookup.txt");
 
        if (!File.Exists(wordLookupFile))    // Contains about 150,000 words
            new WebClient().DownloadFile(
              "http://www.dusystem.com/allwords.txt", wordLookupFile);
 
        var wordLookup = new HashSet<string>(
          File.ReadAllLines(wordLookupFile),
          StringComparer.InvariantCultureIgnoreCase);
 
        string[] wordList = wordLookup.ToArray();
 
        // Here, we're using ThreadLocal to generate a thread-safe random number generator,
        // so we can parallelize the building of the wordsToTest array.
        var localRandom = new ThreadLocal<Random>
          (() => new Random(Guid.NewGuid().GetHashCode()));
 
        string[] wordsToTest = Enumerable.Range(0, 1000000).AsParallel()
          .Select(i => wordList[localRandom.Value.Next(0, wordList.Length)])
          .ToArray();
 
        wordsToTest[12345] = "woozsh";     // Introduce a couple
        wordsToTest[23456] = "wubsie";     // of spelling mistakes.
 
        var query = wordsToTest
          .AsParallel()
          .Select((word, index) => new IndexedWord { Word = word, Index = index })
          .Where(iword => !wordLookup.Contains(iword.Word))
          .OrderBy(iword => iword.Index);
 
        query.Dump();  //Dump
 
 
 
        string text = "Let’s suppose this is a really long string";
 
        int[] result =
          text.AsParallel().Aggregate(
            () => new int[26],             // Create a new local accumulator
 
            (localFrequencies, c) =>       // Aggregate into the local accumulator
    {
                int index = char.ToUpper(c) - 'A';
                if (index >= 0 && index <= 26) localFrequencies[index]++;
                return localFrequencies;
            },
            // Aggregate local->main accumulator
            (mainFreq, localFreq) =>
              mainFreq.Zip(localFreq, (f1, f2) => f1 + f2).ToArray(),
 
            finalResult => finalResult     // Perform any final transformation
          );                                 // on the end result.
 
        result.Dump();
 
 
        Parallel.ForEach(GetPokemon().OrderBy(p => p.Name), (p,s) =>
        {
            Response.Write(String.Format("Changing owner for {0} <br/>", p.Name));
            p.Owner = "Aaron";
        });
 
        Parallel.ForEach(GetPokemon().OrderBy(p => p.Name), (p, s, i) =>
        {
            Response.Write(String.Format("{0}. Changing owner for: {1}<br/>",i,p.Name));
            p.Owner = "Aaron";
        });
 
        GetPokemon().OrderBy(p => p.Name).AsParallel().ForAll((p) =>
        {
            Response.Write(String.Format("Changing owner for Du Name: {0}<br/>", p.Name));
            p.Owner = "Aaron";
        });
 
 
    }
    static IEnumerable<Pokemon> GetPokemon() => new List<Pokemon>
    {
        new Pokemon("Pikachu", "Electric", "Ash"),
        new Pokemon("Bulbasaur", "Grass", "Ash"),
        new Pokemon("Squirtle", "Water", "Ash"),
        new Pokemon("Charmander", "Fire", "Aaron"),
        new Pokemon("Gengar", "Ghost", "Ash"),
        new Pokemon("Snorlax", "Normal", "Ash"),
        new Pokemon("Geovin Du", "Psychic", "Ash"),
    };
 
 
    struct IndexedWord { public string Word; public int Index; }  
 
 
}
 
/// <summary>
///
/// </summary>
public static class Dumper
{
    public static string ToPrettyString(this object value)
    {
        return JsonConvert.SerializeObject(value, Formatting.Indented);
    }
 
    public static T Dump<T>(this T value)
    {
        Console.WriteLine(value.ToPrettyString());
        return value;
    }
}
 
/// <summary>
///
/// </summary>
public class Pokemon
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="name"></param>
    /// <param name="type"></param>
    /// <param name="owner"></param>
    public Pokemon(string name, string type, string owner)
    {
        Name = name;
        Type = type;
        Owner = owner;
    }
    /// <summary>
    ///
    /// </summary>
    public string Name { get; set; }
 
    /// <summary>
    ///
    /// </summary>
    public string Type { get; set; }
    /// <summary>
    ///
    /// </summary>
    public string Owner { get; set; }
}

 

posted @   ®Geovin Du Dream Park™  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示