编程语言   发布时间:2022-06-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Csharp: Parallel LINQ大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

https://devtut.github.io/csharp/parallel-linq-plinq.html#simple-examplehttps://github.com/PacktPublishing/Hands-On-Parallel-ProgrAMMing-with-C-8-and-.NET-Core-3.0https://gist.github.com/gfoidl/d8250ff11a5c70972cb8164622792ba1https://github.com/lassevk/ObjectDumperhttps://docs.microsoft.com/en-us/dotnet/standard/parallel-progrAMMing/order-preservation-in-plinqLINQPadhttps://github.com/giawa/RedPandaOShttp://www.albahari.com/nutsHell/E9-CH22.aspxhttps://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10https://github.com/WolfgangOfner/CSharp-9.0/tree/master/CSharp9https://github.com/apress/beg-cpphttps://github.com/Apress/beginning-cpp20https://github.com/packtpublishing/beginning-cpp-progrAMMing

 

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; }
}

 

大佬总结

以上是大佬教程为你收集整理的Csharp: Parallel LINQ全部内容,希望文章能够帮你解决Csharp: Parallel LINQ所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。