Skip to content

Instantly share code, notes, and snippets.

@mastoj
Last active May 21, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastoj/63dca564a928b9cba890049eae01bf54 to your computer and use it in GitHub Desktop.
Save mastoj/63dca564a928b9cba890049eae01bf54 to your computer and use it in GitHub Desktop.
Some plinq examples
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace ParallelTest
{
class Program
{
static void Main(string[] args)
{
RunTests();
Console.ReadLine();
}
private static async void RunTests()
{
await Test(ToListExample, "ToListExample");
await Test(NoToListExample, "NoToListExample");
await Test(NoParallelExample, "NoParallelExample");
}
private static async Task<int> NoParallelExample()
{
var result =
await Task.WhenAll(Enumerable
.Range(0, 100)
.Select((_, index) => WorkWorkWork(index)));
var sum = result.Sum();
return sum;
}
private static async Task<int> ToListExample()
{
var result =
await Task.WhenAll(Enumerable
.Range(0, 100)
.AsParallel()
.Select((_, index) => WorkWorkWork(index))
.ToList());
var sum = result.Sum();
return sum;
}
private static async Task<int> NoToListExample()
{
var result =
await Task.WhenAll(Enumerable
.Range(0, 100)
.AsParallel()
.Select((_, index) => WorkWorkWork(index)));
var sum = result.Sum();
return sum;
}
private static async Task Test(Func<Task<int>> target, string sampleTest)
{
var sw = new Stopwatch();
sw.Start();
var sum = await target();
Console.WriteLine($"Sum: {sum}");
sw.Stop();
Console.WriteLine($"{sampleTest} took {sw.ElapsedMilliseconds} ms");
}
private static async Task<int> WorkWorkWork(int i)
{
await Task.Delay(1000);
return i;
}
}
}
@kahole
Copy link

kahole commented May 20, 2020

Bare heads-up; taskene starter utenfor Stopwatchen. Det påvirker resultatene litt.
Du burde kanskje gjøre noe sånn som her:

        private static async Task Test(Func<Task<int>> target, string sampleTest)
        {
            var sw = new Stopwatch();
            sw.Start();
            var sum = await target();
            Console.WriteLine($"Sum: {sum}");
            sw.Stop();
            Console.WriteLine($"{sampleTest} took {sw.ElapsedMilliseconds} ms");
        }

Kan illustreres med dette programmet:

        static void Main(string[] args)
        {
            StuffCaller();
        }
        
        private static async void StuffCaller()
        {
            var stuffTask = DoStuffBeforeAwait(6);
            Console.ReadLine();
            var result = await stuffTask;
            Console.WriteLine($"Result {result}");
        }

        private static async Task<int> DoStuffBeforeAwait(int i)
        {
            Console.WriteLine("I'm doing stuff even though await hasnt been called on me yet.");
            await Task.Delay(1000);
            Console.WriteLine("More stuff");
            return i;
        }

@mastoj
Copy link
Author

mastoj commented May 21, 2020

@kahole, you're completely right so I updated the sample. I'm more used to F# way of dealing with async, which doesn't stark the execution until you actually ask for the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment