Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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();
        }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        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();
        }
Esempio n. 5
0
        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");
        }
Esempio n. 6
0
        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());
        }
Esempio n. 7
0
        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();
        }
Esempio n. 8
0
        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();
        }
Esempio n. 9
0
        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();
        }
Esempio n. 10
0
        /// <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();
        }
Esempio n. 11
0
        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();
        }
Esempio n. 12
0
        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();
        }
Esempio n. 13
0
        /// <summary>
        /// 執行同步縮放並計算執行時間
        /// </summary>
        /// <returns>耗時(毫秒)</returns>
        static long CalculateSpendingTimeResize()
        {
            imageProcess.Clean(destinationPath);
            Stopwatch sw = new Stopwatch();

            sw.Start();
            imageProcess.ResizeImages(sourcePath, destinationPath, resizeRatio);
            sw.Stop();
            return(sw.ElapsedMilliseconds);
        }
Esempio n. 14
0
        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();
        }
Esempio n. 15
0
        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();
        }
Esempio n. 16
0
        static async Task SyncTest()
        {
            string sourcePath            = Path.Combine(Environment.CurrentDirectory, "images");
            string destinationPath       = Path.Combine(Environment.CurrentDirectory, "output");;
            string asyncDestinationPath  = Path.Combine(Environment.CurrentDirectory, "outputasync");;
            string asyncDestinationPath2 = Path.Combine(Environment.CurrentDirectory, "outputasync2");;
            string asyncDestinationPath3 = Path.Combine(Environment.CurrentDirectory, "outputasync3");;

            ImageProcess imageProcess = new ImageProcess();

            imageProcess.Clean(destinationPath);
            imageProcess.Clean(asyncDestinationPath);
            imageProcess.Clean(asyncDestinationPath2);
            imageProcess.Clean(asyncDestinationPath3);

            Stopwatch sw = new Stopwatch();

            sw.Start();
            imageProcess.ResizeImages(sourcePath, destinationPath, 2.0);
            sw.Stop();
            double syncgms = sw.ElapsedMilliseconds;

            Console.WriteLine($"同步花費時間: {sw.ElapsedMilliseconds} ms\r\n");

            // =====================================================================
            sw.Restart();
            await imageProcess.ResizeImagesAsyncI(sourcePath, asyncDestinationPath, 2.0);

            sw.Stop();
            double asyncms = sw.ElapsedMilliseconds;

            Console.WriteLine($"非同步(tasks.whenall+最後異步方法)花費時間: {sw.ElapsedMilliseconds} ms");
            Console.WriteLine($"效能提升百分比{Math.Round((1 - (asyncms / syncgms)) * 100)  }%\r\n");
            // =====================================================================
            sw.Restart();
            imageProcess.ResizeImagesAsyncII(sourcePath, asyncDestinationPath2, 2.0);
            sw.Stop();
            double asyncmsII = sw.ElapsedMilliseconds;

            Console.WriteLine($"非同步(tasks.WaitAll+最後同步方法)花費時間: {sw.ElapsedMilliseconds} ms");
            Console.WriteLine($"效能提升百分比{Math.Round((1 - (asyncmsII / syncgms)) * 100)  }% (這個永遠比較快一點點)\r\n");
            // =====================================================================
            sw.Restart();
            imageProcess.ResizeImagesAsyncIII(sourcePath, asyncDestinationPath3, 2.0);
            sw.Stop();
            double asyncmsIII = sw.ElapsedMilliseconds;

            Console.WriteLine($"非同步(Parallel.ForEach)花費時間: {sw.ElapsedMilliseconds} ms");
            Console.WriteLine($"效能提升百分比{Math.Round((1 - (asyncmsIII / syncgms)) * 100)  }%\r\n");
            // =====================================================================
        }
Esempio n. 17
0
        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);
            Console.WriteLine($"原始花費時間: {sw.ElapsedMilliseconds} ms");
            sw.Stop();

            //imageProcess.Clean(destinationPath);
            //sw.Restart();
            //imageProcess.ResizeImagesTask(sourcePath, destinationPath, 2.0);
            //sw.Stop();
            //Console.WriteLine($"Task: {sw.ElapsedMilliseconds} ms");

            //imageProcess.Clean(destinationPath);
            //sw.Restart();
            //await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0);
            //sw.Stop();
            //Console.WriteLine($"Async花費時間: {sw.ElapsedMilliseconds} ms");

            //imageProcess.Clean(destinationPath);
            //sw.Restart();
            //imageProcess.ResizeImagesParallel(sourcePath, destinationPath, 2.0);
            //sw.Stop();

            //Console.WriteLine($"Parallel花費時間: {sw.ElapsedMilliseconds} ms");

            imageProcess.Clean(destinationPath);
            sw.Restart();
            await imageProcess.ResizeImagesParallelForEach(sourcePath, destinationPath, 2.0);

            sw.Stop();

            Console.WriteLine($"ParallelForEach花費時間: {sw.ElapsedMilliseconds} ms");

            //imageProcess.Clean(destinationPath);
            //sw.Restart();
            //await imageProcess.ResizeImagesParallelForEachNest(sourcePath, destinationPath, 2.0);
            //sw.Stop();

            //Console.WriteLine($"ResizeImagesParallelForEachNest花費時間: {sw.ElapsedMilliseconds} ms");
        }
Esempio n. 18
0
        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");
        }
Esempio n. 19
0
        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();
        }
Esempio n. 20
0
        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();
        }
Esempio n. 21
0
        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();
        }
Esempio n. 22
0
        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");
        }
Esempio n. 23
0
        //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%
        }
Esempio n. 24
0
        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}");
            }
        }
Esempio n. 25
0
        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();
        }
Esempio n. 26
0
        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%
        }
Esempio n. 27
0
        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();
        }
Esempio n. 28
0
        static async Task Main(string[] args)
        {
            #region 設備

            int coreCount = 0;
            foreach (var item in new ManagementObjectSearcher("SELECT * FROM Win32_Processor").Get())
            {
                coreCount += int.Parse(item["NumberOfCores"].ToString());
            }
            Console.WriteLine($"核心數: {coreCount}");
            Console.WriteLine($"邏輯處理器數: {Environment.ProcessorCount}");

            #endregion

            string sourcePath      = Path.Combine(Environment.CurrentDirectory, "images");
            string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");;

            ImageProcess imageProcess = new ImageProcess();
            Stopwatch    sw           = new Stopwatch();

            #region  步

            imageProcess.Clean(destinationPath);
            sw.Start();
            imageProcess.ResizeImages(sourcePath, destinationPath, 2.0);
            sw.Stop();
            var syncTime = sw.ElapsedMilliseconds;
            Console.WriteLine($"同步處理花費時間: {syncTime} ms");

            #endregion

            #region 非同步

            imageProcess.Clean(destinationPath);
            sw.Restart();
            await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0);

            sw.Stop();
            var asyncTime = sw.ElapsedMilliseconds;

            Console.WriteLine($"非同步處裡花費時間: {asyncTime} ms");
            Console.WriteLine($"提升效率: {((syncTime - asyncTime) / (double)syncTime * 100).ToString("#.#")} %");
            Console.ReadKey();

            #endregion
        }
Esempio n. 29
0
        static async Task Main(string[] args)
        {
            string sourcePath      = Path.Combine(Environment.CurrentDirectory, "images");
            string destinationPath = Path.Combine(Environment.CurrentDirectory, "output");;

            ImageProcess imageProcess = new ImageProcess();

            for (int index = 0; index < 10; index++)
            {
                imageProcess.Clean(destinationPath);
                Stopwatch sw1 = new Stopwatch();
                Stopwatch sw2 = new Stopwatch();
                Stopwatch sw3 = new Stopwatch();

                // Original
                imageProcess.Clean(destinationPath);
                sw1.Start();
                imageProcess.ResizeImages(sourcePath, destinationPath, 2.0);
                sw1.Stop();
                GC.Collect();
                Thread.Sleep(3000);

                // PLINQ
                imageProcess.Clean(destinationPath);
                sw2.Start();
                imageProcess.ResizeImagesPLINQ(sourcePath, destinationPath, 2.0);
                sw2.Stop();
                GC.Collect();
                Thread.Sleep(3000);

                // Async
                imageProcess.Clean(destinationPath);
                sw3.Start();
                imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0);
                sw3.Stop();
                GC.Collect();
                Thread.Sleep(3000);

                Console.WriteLine($"Original - 第{index+1}次花費時間: {sw1.ElapsedMilliseconds} ms");
                Console.WriteLine($"PLINQ - 第{index+1}次花費時間: {sw2.ElapsedMilliseconds} ms");
                Console.WriteLine($"Async - 第{index+1}次花費時間: {sw3.ElapsedMilliseconds} ms");
                Console.WriteLine($"========================================================");
            }
        }
Esempio n. 30
0
        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 sw1 = new Stopwatch();

            sw1.Start();
            imageProcess.ResizeImages(sourcePath, destinationPath, 2.0);
            sw1.Stop();

            imageProcess.Clean(destinationPath);

            Stopwatch sw2 = new Stopwatch();

            sw2.Start();
            imageProcess.ResizeImagesWait(sourcePath, destinationPath, 2.0);
            sw2.Stop();

            imageProcess.Clean(destinationPath);

            Stopwatch sw3 = new Stopwatch();

            sw3.Start();
            await imageProcess.ResizeImagesAsync(sourcePath, destinationPath, 2.0);

            sw3.Stop();

            Console.WriteLine($"花費時間          : {sw1.ElapsedMilliseconds} ms");

            double percent1 = Math.Round((sw1.ElapsedMilliseconds - sw2.ElapsedMilliseconds) * 1.00 / sw1.ElapsedMilliseconds * 100.0, 1);

            Console.WriteLine($"花費時間 WaitAll  : {sw2.ElapsedMilliseconds} ms, 提升: {percent1} %");

            double percent2 = Math.Round((sw1.ElapsedMilliseconds - sw3.ElapsedMilliseconds) * 1.00 / sw1.ElapsedMilliseconds * 100.0, 1);

            Console.WriteLine($"花費時間 WhenAll  : {sw3.ElapsedMilliseconds} ms, 提升: {percent2} %");
        }