CSharp: Parallel Extensions

 

  /// <summary>
    /// Parallel Extensions
    /// </summary>
    class Program
    {
        // 数量配置
        private const int NUM_AES_KEYS = 800000;
        private const int NUM_MD5_HASHES = 100000;

        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        private static string ConvertToHexString(Byte[] byteArray)
        {
            var sb = new StringBuilder(byteArray.Length);
            for (int i = 0; i < byteArray.Length; i++)
            {
                sb.Append(byteArray[i].ToString("X2"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成 AES 密钥(.NET 4.0 必须用 AesManaged)
        /// </summary>
        private static void GenerateAESKeys()
        {
            var sw = Stopwatch.StartNew();

            // .NET 4.0 写法
            using (var aesM = new AesManaged())
            {
                for (int i = 1; i <= NUM_AES_KEYS; i++)
                {
                    aesM.GenerateKey();
                    byte[] result = aesM.Key;
                    string hexString = ConvertToHexString(result);
                }
            }

            // 直接输出到控制台,看得见!
            Console.WriteLine("AES 任务耗时:" + sw.Elapsed);
        }

        /// <summary>
        /// 生成 MD5 哈希
        /// </summary>
        private static void GenerateMD5Hashes()
        {
            var sw = Stopwatch.StartNew();

            using (var md5M = MD5.Create())
            {
                for (int i = 1; i <= NUM_MD5_HASHES; i++)
                {
                    byte[] data = Encoding.Unicode.GetBytes(Environment.UserName + i.ToString());
                    byte[] result = md5M.ComputeHash(data);
                    string hexString = ConvertToHexString(result);
                }
            }

            Console.WriteLine("MD5 任务耗时:" + sw.Elapsed);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("===== .NET 4.0 并行执行演示 =====");
            Console.WriteLine("→ AES 和 MD5 会同时运行,不是先后运行!\n");

            // 开始计时
            var totalSw = Stopwatch.StartNew();

            // ==================== 核心 ====================
            // Parallel.Invoke = 同时执行多个方法(并行)
            // ==============================================
            Parallel.Invoke(
                () => GenerateAESKeys(),   // 任务1
                () => GenerateMD5Hashes()  // 任务2
            );

            totalSw.Stop();

            // 输出总时间
            Console.WriteLine("\n===== 运行完成 =====");
            Console.WriteLine("并行总耗时:" + totalSw.Elapsed);

            Console.WriteLine("\n按回车退出");
            Console.ReadLine();
        }
    }

  

https://github.com/deepmindd/Cppbooks/blob/main/C%2B%2B%20Concurrency%20in%20Action.pdf
https://github.com/anthonywilliams/ccia_code_samples
https://github.com/xiaoweiChen/Cpp_Concurrency_In_Action
https://github.com/jeremimucha/Cpp-Concurrency-In-Action-2nd-ed
https://github.com/iZhangHui/CCiA
https://github.com/ZouJiu1/multithread_Cplusplus
https://beefnoodles.cc/assets/book/C++%20Concurrency%20in%20Action.pdf
https://www.manning.com/books/concurrency-in-dot-net
https://github.com/subhajitchatterjee07/Concurrency-in-Python
https://github.com/concurrency-in-python-with-asyncio/concurrency-in-python-with-asyncio

https://www.wiley.com/en-us/Essential+Algorithms%3A+A+Practical+Approach+to+Computer+Algorithms+Using+Python+and+C%23%2C+2nd+Edition-p-9781119575993#downloadstab-section
https://ia803202.us.archive.org/31/items/python_ebooks_2020/%5BRod_Stephens%5D_Essential_Algorithms__A_Practical_A.pdf
https://github.com/Belowbelow/ComputerScienceBooks
https://github.com/GrindGold/pdf
https://github.com/SZU-ITer/CS-Books-PDF

https://github.com/jeremiahflaga/concurrency-in-csharp-cookbook-2e
https://www.packtpub.com/en-in/product/hands-on-parallel-programming-with-c-8-and-net-core-3-9781789132410/chapter/introduction-to-parallel-programming-2/section/introduction-to-parallel-programming-ch02lvl1sec02
https://github.com/wagnerhsu/packt-Parallel-Programming-and-Concurrency-with-C-sharp-10-and-.NET-6/
https://github.com/Apress/Parallel-Programming-with-CSharp-and-.NET

posted @ 2026-03-29 11:28  ®Geovin Du Dream Park™  阅读(9)  评论(0)    收藏  举报