static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); var cts = new CancellationTokenSource(); #region 等候使用者輸入 取消 c 按鍵 ThreadPool.QueueUserWorkItem(x => { var key = Console.ReadKey(); if (key.Key == ConsoleKey.C) { cts.Cancel(); } }); #endregion ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); // imageProcess.ResizeImages(sourcePath, destinationPath, 2.0d); // await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0d, CancellationToken.None); await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0d, cts.Token); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); var before = 0D; var after = 0D; var diff = 0D; for (int i = 1; i <= 3; i++) { Console.WriteLine($"第{i}次測試"); sw.Restart(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); before = sw.ElapsedMilliseconds; Console.WriteLine($"原來花費時間: {before} ms"); sw.Restart(); imageProcess.ParallelResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); after = sw.ElapsedMilliseconds; diff = (before - after) / before * 100; Console.WriteLine($"修改後花費時間: {after} ms 效能提升:{diff:N2}%"); } Console.ReadKey(); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); #region 原始版本 Stopwatch sw1 = new Stopwatch(); sw1.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw1.Stop(); #endregion imageProcess.Clean(destinationPath); #region 非同步版本 Stopwatch sw2 = new Stopwatch(); sw2.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw2.Stop(); #endregion Console.WriteLine($"原始版本_花費時間: {sw1.ElapsedMilliseconds} ms"); Console.WriteLine($"非同步版本_花費時間: {sw2.ElapsedMilliseconds} ms"); Console.WriteLine(string.Format("效能提升百分比:{0} %", ((double)(sw1.ElapsedMilliseconds - sw2.ElapsedMilliseconds) / (double)sw1.ElapsedMilliseconds)).ToString()); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); string destinationPathAsync = Path.Combine(Environment.CurrentDirectory, "outputAsync"); ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); imageProcess.Clean(destinationPathAsync); Stopwatch SyncSW = new Stopwatch(); SyncSW.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); SyncSW.Stop(); Stopwatch AsyncSW = new Stopwatch(); AsyncSW.Start(); var tList = imageProcess.ResizeImagesAsync(sourcePath, destinationPathAsync, 2.0); await Task.WhenAll(tList); AsyncSW.Stop(); Console.WriteLine($"同步花費時間: {SyncSW.ElapsedMilliseconds} ms"); Console.WriteLine($"非同步花費時間: {AsyncSW.ElapsedMilliseconds} ms"); double e = SyncSW.ElapsedMilliseconds - AsyncSW.ElapsedMilliseconds; Console.WriteLine($"效能快了: {Math.Round((double)(e / (double)SyncSW.ElapsedMilliseconds), 4) * 100}% "); Console.ReadKey(); }
/// <summary> /// Main /// </summary> /// <param name="args"></param> /// <returns></returns> static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); // 同步 Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); var TaskSec = sw.ElapsedMilliseconds; Console.WriteLine($"[同步] 花費時間: {sw.ElapsedMilliseconds} ms"); imageProcess.Clean(destinationPath); // 非同步 sw.Restart(); await imageProcess.ResizeImagesByAsync(sourcePath, destinationPath, 2.0); sw.Stop(); var AsyncTaskSec = sw.ElapsedMilliseconds; Console.WriteLine($"[非同步] 花費時間: {AsyncTaskSec} ms"); Console.WriteLine($"提升 : {((TaskSec - AsyncTaskSec) / (double)TaskSec * 100).ToString("#.#")} %"); Console.ReadKey(); }
static void Main(string[] args) { Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelJob); string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); //sw.Start(); //imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); //sw.Stop(); //Console.WriteLine($"Sync: {sw.ElapsedMilliseconds} ms"); sw.Reset(); sw.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0, cts.Token); sw.Stop(); Console.WriteLine($"Async: {sw.ElapsedMilliseconds} ms"); }
static void Main() { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); var before = 0D; var after = 0D; var diff = 0D; for (int i = 1; i <= 5; i++) { sw.Restart(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); before = sw.ElapsedMilliseconds; Console.WriteLine($"花費時間: {before} ms"); sw.Restart(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); after = sw.ElapsedMilliseconds; diff = (before - after) / before * 100; Console.WriteLine($"Async花費時間: {after} ms, {diff:N2}% ... {i}"); } }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); // 非同步 imageProcess.Clean(destinationPath); Stopwatch sw1 = new Stopwatch(); sw1.Start(); imageProcess.ResizeImagesAync(sourcePath, destinationPath, 2.0); sw1.Stop(); Console.WriteLine($"非同步花費時間: {sw1.ElapsedMilliseconds} ms"); double result = Convert.ToDouble((sw.ElapsedMilliseconds - sw1.ElapsedMilliseconds) * 100) / sw.ElapsedMilliseconds; Console.WriteLine($"效能提升百分比: {result} %"); Console.ReadKey(); }
private static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); //未調整前 imageProcess.Clean(destinationPath); Stopwatch sw1 = new Stopwatch(); sw1.Start(); imageProcess.OldResizeImages(sourcePath, destinationPath, 2.0); sw1.Stop(); Console.WriteLine($"舊方法花費時間: {sw1.ElapsedMilliseconds} ms"); //調整後 imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); Stopwatch sw1 = new Stopwatch(); sw1.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw1.Stop(); Console.WriteLine($"原始花費時間: {sw.ElapsedMilliseconds} ms"); Console.WriteLine($"非同步花費時間: {sw1.ElapsedMilliseconds} ms"); Console.WriteLine($"節省約: {(double)(sw.ElapsedMilliseconds - sw1.ElapsedMilliseconds) / (double)sw.ElapsedMilliseconds} % "); Console.ReadKey(); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); long orig = 0; //原始 imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); orig = sw.ElapsedMilliseconds; Console.WriteLine("原始花費時間:" + orig + "ms"); long _new = 0; //非同步 imageProcess.Clean(destinationPath); sw.Restart(); //Console.WriteLine("縮放作業開始: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff")); await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); //Console.WriteLine("縮放作業結束: " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff")); sw.Stop(); _new = sw.ElapsedMilliseconds; Console.WriteLine("非同步花費時間:" + _new + "ms"); Console.WriteLine("效能 " + Math.Floor((double)(orig - _new) / orig * 100) + "%"); Console.ReadKey(); }
//main 變成非同步 會變8ms 但是沒有圖片 static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += (sender, msg) => HandleUnhandledException(msg.ExceptionObject as Exception); string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destPath); double orig = GetExecutionTime(() => imageProcess.ResizeImages(sourcePath, destPath, 2.0)); imageProcess = new ImageProcess(); imageProcess.Clean(destPath); double @new = GetExecutionTime(() => imageProcess.ResizeImagesAsync(sourcePath, destPath, 2.0).Wait()); Console.WriteLine($"花費時間: {orig} | {@new} ms,{((orig - @new) / orig).ToString("P")}"); //E3-1230v3 邏輯處理器 8核 //1. 花費時間: 3672 | 2311 ms,37.06% //2. 花費時間: 3363 | 2277 ms,32.29% //3. 花費時間: 3359 | 2319 ms,30.96% //4. 花費時間: 3323 | 2367 ms,28.77% //5. 花費時間: 3456 | 2490 ms,27.95% //6. 花費時間: 3378 | 2209 ms,34.61% //7. 花費時間: 3487 | 2376 ms,31.86% //8. 花費時間: 3294 | 2341 ms,28.93% //9. 花費時間: 3521 | 2396 ms,31.95% //10.花費時間: 3769 | 2266 ms,39.88% }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(3000); try { Stopwatch sw = new Stopwatch(); sw.Start(); await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0, cts.Token); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); } catch (OperationCanceledException) { Console.WriteLine($"{Environment.NewLine}下載已經取消"); } catch (Exception) { Console.WriteLine($"{Environment.NewLine}下載發現例外異常,已經中斷"); } Console.ReadKey(); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); //同步版 //ImageProcess OriImageProcess2 = new ImageProcess(); //OriImageProcess2.Clean(destinationPath); //Stopwatch OriWatch = new Stopwatch(); //OriWatch.Start(); //OriImageProcess2.ResizeImages(sourcePath, destinationPath, 2.0); //OriWatch.Stop(); //Console.WriteLine($"花費時間: {OriWatch.ElapsedMilliseconds} ms"); //非同步版 ImageProcess imageProcess = new ImageProcess(); imageProcess.CleanAsync(destinationPath).Wait(); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0).Wait(); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); Console.Read(); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); Console.WriteLine("Running..."); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); //var task = imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); //task.Wait(); //imageProcess.ResizeImagesParallel(sourcePath, destinationPath, 2.0); //var task = imageProcess.ResizeImagesParallelAsync(sourcePath, destinationPath, 2.0); //task.Wait(); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); Console.ReadLine(); }
private static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); await imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"花費時間: {sw.ElapsedMilliseconds} ms"); // 調整前 3085ms // 調整後 2325ms // ~75% // 調整前 5860ms // 調整後 4100ms // ~70% }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); float ori = sw.ElapsedMilliseconds; imageProcess.Clean(destinationPath); Stopwatch sw2 = new Stopwatch(); sw2.Start(); var task = imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw2.Stop(); float asy = sw2.ElapsedMilliseconds; Console.WriteLine($"同步花費時間: {ori} ms"); Console.WriteLine($"異步花費時間: {asy} ms"); Console.WriteLine($"花費時間百分比: {(ori - asy) / ori * 100} %"); Console.ReadLine(); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); string destination2Path = Path.Combine(Environment.CurrentDirectory, "output2"); ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); imageProcess.Clean(destination2Path); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"修改前花費時間: {sw.ElapsedMilliseconds} ms"); sw.Restart(); await imageProcess.ResizeImagesAsync(sourcePath, destination2Path, 2.0); sw.Stop(); Console.WriteLine($"修改後花費時間: {sw.ElapsedMilliseconds} ms"); Console.Read(); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); double oldts = sw.Elapsed.TotalMilliseconds; Console.WriteLine($"Sync Times: {oldts} ms"); imageProcess.Clean(destinationPath); sw.Restart(); await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); double newts = sw.Elapsed.TotalMilliseconds; Console.WriteLine($"Async Times: {newts} ms"); Console.WriteLine($"Improvement Ratio: {Math.Round(((oldts - newts) / oldts) * 100, 0)} %"); Console.ReadKey(); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output"); ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); var oriTime = sw.ElapsedMilliseconds; imageProcess.Clean(destinationPath); Console.WriteLine($"同步-花費時間: {oriTime} ms"); sw.Restart(); await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); var asyncTime = sw.ElapsedMilliseconds; Console.WriteLine($"非同步-花費時間: {asyncTime} ms"); var improve = ((float)(oriTime - asyncTime) / oriTime) * 100; Console.WriteLine($"{improve}%"); Console.ReadKey(); }
static async Task Main(string[] args) { Stopwatch sw = new Stopwatch(); string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); var Orig = sw.ElapsedMilliseconds; Console.WriteLine($"同步(Synchronous)花費時間: {sw.ElapsedMilliseconds} ms"); sw.Reset(); imageProcess.Clean(destinationPath); sw.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); var New = sw.ElapsedMilliseconds; Console.WriteLine($"非同步(Asynchronous)花費時間: {sw.ElapsedMilliseconds} ms"); Console.WriteLine("Performance ratio :" + ((Orig - New) / (double)Orig * 100).ToString("0.00") + "%"); Console.ReadKey(); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; var imageProcess = new ImageProcess(); var imageProcessAsync1 = new ImageProcessAsync1(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); var timeSync = sw.ElapsedMilliseconds; Console.WriteLine($"同步版花費時間: {sw.ElapsedMilliseconds} ms"); sw.Reset(); sw.Start(); imageProcessAsync1.ResizeImagesAsync(sourcePath, destinationPath, 2.0).Wait(); sw.Stop(); var timeAsync = sw.ElapsedMilliseconds; Console.WriteLine($"非同步版花費時間: {sw.ElapsedMilliseconds} ms"); var comparison = ((double)(timeSync - timeAsync) / timeSync) * 100; Console.WriteLine($"節省: {comparison:N2} % 作業時間"); Console.ReadKey(); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw1 = new Stopwatch(); sw1.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw1.Stop(); Console.WriteLine($"修改前花費時間: {sw1.ElapsedMilliseconds} ms"); imageProcess.Clean(destinationPath); Stopwatch sw2 = new Stopwatch(); sw2.Start(); var result = imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); Task.WaitAll(result); sw2.Stop(); Console.WriteLine($"修改後花費時間: {sw2.ElapsedMilliseconds} ms"); Console.ReadKey(); }
private static Task <string> Test() { Task <string> task = Task.Run(() => { ImageProcess.Write("This is my string."); return("sssssssss"); }); return(task); }
//測試環境:筆電 i7-6500U、20 GB DDR4 RAM 、SanDisk SD8SN8U512G1002 M2 SSD //測試檔案: 67個jpg檔,總共30.2MB ,VS組建組態:Debug //調整前:42829ms , 42942ms ,42712ms =>平均42828ms //調整後(ThreadPool版本):28251ms , 25335ms , 29868ms =>平均27818ms ,速度提升35% static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); Console.ReadLine(); }
/// <summary> /// 計時圖片的縮放作業(平行) /// </summary> static void TimingP() { string destinationPath = Path.Combine(Environment.CurrentDirectory, "output-p"); ImageProcess imageProcess = new ImageProcess(); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages_Parallel(SourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"平行花費時間: {sw.ElapsedMilliseconds} ms"); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); Stopwatch sw = new Stopwatch(); for (int i = 1; i <= 10; i++) { // Sync imageProcess.Clean(destinationPath); sw.Restart(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); var syncMs = sw.ElapsedMilliseconds; Console.WriteLine($"第{i}次 Sync 花費時間: {syncMs} ms"); // Sync //imageProcess.Clean(destinationPath); //sw.Restart(); //imageProcess.ResizeImagesPLNQ(sourcePath, destinationPath, 2.0); //sw.Stop(); //var syncPLINQMs = sw.ElapsedMilliseconds; //Console.WriteLine($"第{i}次 Sync with PLINQ 花費時間: {syncPLINQMs} ms"); // Async imageProcess.Clean(destinationPath); sw.Restart(); await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); var asyncMs = sw.ElapsedMilliseconds; Console.WriteLine($"第{i}次 Async 花費時間: {asyncMs} ms"); Console.WriteLine($"調整比率: {((syncMs - asyncMs) / (double)syncMs * 100).ToString("#.#")} %"); // Async with PLINQ //imageProcess.Clean(destinationPath); //sw.Restart(); //await imageProcess.ResizeImagesPLNQAsync(sourcePath, destinationPath, 2.0); //sw.Stop(); //var asyncPLINQMs = sw.ElapsedMilliseconds; //Console.WriteLine($"第{i}次 Async with PLINQ 花費時間: {asyncPLINQMs} ms"); //Console.WriteLine($"調整比率: {((syncMs - asyncPLINQMs) / (double)syncMs * 100).ToString("#.#")} %"); } Console.ReadKey(); }
static async Task Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; ImageProcess imageProcess = new ImageProcess(); imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); var timeCost_Ori = sw.ElapsedMilliseconds; Console.WriteLine($"花費時間: {timeCost_Ori} ms"); sw.Reset(); sw.Start(); imageProcess.ResizeImages_V1(sourcePath, destinationPath, 2.0); sw.Stop(); var timeCost_V1 = sw.ElapsedMilliseconds; Console.WriteLine($"V1 花費時間: {timeCost_V1} ms ( WaitAll ) "); Console.WriteLine($"V1 節省約: {(double)(timeCost_Ori - timeCost_V1) / (double)timeCost_Ori} % "); sw.Reset(); sw.Start(); await imageProcess.ResizeImages_V2(sourcePath, destinationPath, 2.0); sw.Stop(); var timeCost_V2 = sw.ElapsedMilliseconds; Console.WriteLine($"V2 花費時間: {timeCost_V2} ms ( await WhenAll )"); Console.WriteLine($"V2 節省約: {(double)(timeCost_Ori - timeCost_V2) / (double)timeCost_Ori} % "); sw.Reset(); sw.Start(); imageProcess.ResizeImages_V3(sourcePath, destinationPath, 2.0); sw.Stop(); var timeCost_V3 = sw.ElapsedMilliseconds; Console.WriteLine($"V3 花費時間: {timeCost_V3} ms ( Parallel.ForEach )"); Console.WriteLine($"V3 節省約: {(double)(timeCost_Ori - timeCost_V3) / (double)timeCost_Ori} % "); Console.WriteLine($"PressAnyKey to End"); Console.ReadKey(); }
static private long RunImageProcessAsync(ref ImageProcess imageProcess, string sourcePath, string destinationPath) { imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0).Wait(); sw.Stop(); return(sw.ElapsedMilliseconds); }
static void Main(string[] args) { string sourcePath = Path.Combine(Environment.CurrentDirectory, "images"); string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");; long SyncAveTime = 0; long AsyncAveTime = 0; int countAll = 0; ImageProcess imageProcess = new ImageProcess(); Console.WriteLine("同步時間Start"); for (var i = 0; i < 20; i++) { imageProcess.Clean(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImages(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"第{i+1}次花費時間: {sw.ElapsedMilliseconds} ms"); SyncAveTime += sw.ElapsedMilliseconds; countAll++; } Console.WriteLine("同步時間End\n"); SyncAveTime = SyncAveTime / countAll; countAll = 0; Console.WriteLine($"同步平均時間:{SyncAveTime} ms\n"); imageProcess.Clean(destinationPath); Console.WriteLine("非同步時間Start"); for (var i = 0; i < 20; i++) { imageProcess.CleanAsync(destinationPath); Stopwatch sw = new Stopwatch(); sw.Start(); imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0); sw.Stop(); Console.WriteLine($"第{i+1}次花費時間: {sw.ElapsedMilliseconds} ms"); AsyncAveTime += sw.ElapsedMilliseconds; countAll++; } Console.WriteLine("非同步時間End\n"); AsyncAveTime = AsyncAveTime / countAll; countAll = 0; Console.WriteLine($"非同步平均時間:{AsyncAveTime} ms\n"); Console.WriteLine(string.Format("提升百分比:{0} %", ((double)(SyncAveTime - AsyncAveTime) / (double)SyncAveTime) * 100).ToString()); //_log.Trace(sw.ElapsedMilliseconds); Console.ReadKey(); }