Skip to content

Instantly share code, notes, and snippets.

@zestime
Created January 24, 2018 22:27
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 zestime/0952363bd0afceb88b98d77f3ea9a116 to your computer and use it in GitHub Desktop.
Save zestime/0952363bd0afceb88b98d77f3ea9a116 to your computer and use it in GitHub Desktop.
Parallel vs PLINQ
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace AsyncTest
{
class Program
{
static void Main(string[] args)
{
RunPLinq();
//RunParallel();
}
static void RunPLinq()
{
Stopwatch span = Stopwatch.StartNew();
GetUrls()
.Select(async url => await Download(url))
.Select(i => i.Result)
.Select(img => new
{
mainImg = ResizeImage(img, 600, 600),
smallImg = ResizeImage(img, 300, 300)
})
.AsParallel()
.ToList();
Console.WriteLine("WTF :" + span.Elapsed);
Console.ReadLine();
}
static void RunParallel()
{
Stopwatch span = Stopwatch.StartNew();
var exceptions = new ConcurrentQueue<Exception>();
// Execute the complete loop and capture all exceptions.
Parallel.ForEach(GetUrls(), async url =>
{
try
{
var image = await Download(url);
var mainImg = ResizeImage(image, 600, 600);
var smallImg = ResizeImage(image, 300, 300);
Console.WriteLine("WTF :" + url);
Console.WriteLine("WTF :" + span.Elapsed);
}
// Store the exception and continue with the loop.
catch (Exception e) { exceptions.Enqueue(e); }
});
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
Console.WriteLine("WTF :" + span.Elapsed);
Console.ReadLine();
}
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
private static IEnumerable<string> GetUrls()
{
var urls = new string[]
{
"https://assets.miumiu.com/content/images/products/details/5BH609_2EJA_F068Z_V_DJM-1.jpg",
"https://assets.miumiu.com/content/images/products/details/5BH609_2EJA_F068Z_V_DJM-2.jpg",
"https://assets.miumiu.com/content/images/products/details/5BH609_2EJA_F068Z_V_DJM-3.jpg",
"https://assets.miumiu.com/content/images/products/details/5BH609_2EJA_F068Z_V_DJM-4.jpg"
};
foreach (string item in urls)
{
Console.WriteLine("Who Am I?");
yield return item;
}
}
private static async Task<Bitmap> Download(string url)
{
WebClient client = new WebClient();
var bytes = await client.DownloadDataTaskAsync(url);
Console.WriteLine(url);
return new Bitmap(new MemoryStream(bytes));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment