Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;

            var imageProcess     = new SKImageProcess();
            var sourcePath       = Path.Combine(Environment.CurrentDirectory, "images");
            var destinationPath1 = Path.Combine(Environment.CurrentDirectory, "output1");
            var destinationPath2 = Path.Combine(Environment.CurrentDirectory, "output2");

            // Sync

            imageProcess.Clean(destinationPath1);

            sw.Start();
            //imageProcess.ResizeImages(sourcePath, destinationPath1, 2.0);
            sw.Stop();

            decimal result1 = sw.ElapsedMilliseconds;

            Console.WriteLine($"同步執行花費時間: {result1} ms");

            // Async

            imageProcess.Clean(destinationPath2);

            sw.Restart();

            try
            {
                Console.WriteLine($"開始非同步執行");
                await imageProcess.ResizeImagesAsync(sourcePath, destinationPath2, 2.0, cts.Token);
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine($"Canceled: {ex}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception:{ex}");
            }

            sw.Stop();

            decimal result2 = sw.ElapsedMilliseconds;

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine($"非同步的花費時間: {result2} ms");

            // Result
            // 效能提升比例公式:((Orig - New) / Orig) * 100%

            if (result1 != 0)
            {
                var result = ((result1 - result2) / result1) * 100;
                Console.WriteLine($"效能提升 {result:f2}%");
            }
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static async Task Main(string[] args)
        {
            // Ctrl + c
            Console.CancelKeyPress += ((object sender, ConsoleCancelEventArgs e) => {
                cts.Cancel();

                e.Cancel = true;
            });

            var imageProcess     = new SKImageProcess();
            var sourcePath       = Path.Combine(Environment.CurrentDirectory, "images");
            var destinationPath1 = Path.Combine(Environment.CurrentDirectory, "output1");
            var destinationPath2 = Path.Combine(Environment.CurrentDirectory, "output2");

            // Sync

            imageProcess.Clean(destinationPath1);

            sw.Start();
            imageProcess.ResizeImages(sourcePath, destinationPath1, 2.0);
            sw.Stop();

            decimal result1 = sw.ElapsedMilliseconds;

            Console.WriteLine($"同步執行花費時間: {result1} ms");

            // Async

            imageProcess.Clean(destinationPath2);

            sw.Restart();

            try
            {
                await imageProcess.ResizeImagesAsync(sourcePath, destinationPath2, 2.0, cts.Token);
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine($"Canceled: {ex}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception:{ex}");
            }

            sw.Stop();

            decimal result2 = sw.ElapsedMilliseconds;

            Console.WriteLine($"非同步的花費時間: {result2} ms");

            // Result
            // 效能提升比例公式:((Orig - New) / Orig) * 100%

            var result = ((result1 - result2) / result1) * 100;

            Console.WriteLine($"效能提升 {result:f2}%");
        }