Program.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. // 提示用户程序开始
  10. Console.WriteLine("开始测试 CPU 性能,请稍等...");
  11. // 获取当前时间戳
  12. var stopwatch = new Stopwatch();
  13. // 单核性能测试
  14. Console.WriteLine("\n开始单核性能测试...");
  15. stopwatch.Start();
  16. TestSingleCorePerformance();
  17. stopwatch.Stop();
  18. Console.WriteLine($"单核测试耗时:{stopwatch.ElapsedMilliseconds} 毫秒");
  19. // 多核性能测试
  20. Console.WriteLine("\n开始多核性能测试...");
  21. stopwatch.Restart();
  22. TestMultiCorePerformance();
  23. stopwatch.Stop();
  24. Console.WriteLine($"多核测试耗时:{stopwatch.ElapsedMilliseconds} 毫秒");
  25. // 暂停程序,等待用户查看结果
  26. Console.WriteLine("按任意键退出...");
  27. Console.ReadKey();
  28. }
  29. // 单核性能测试:在一个线程上执行计算
  30. static void TestSingleCorePerformance()
  31. {
  32. int iterations = 10000000; // 迭代次数,用于增加计算量
  33. // 1. 递归斐波那契数列
  34. Console.WriteLine("计算递归斐波那契数列...");
  35. var fibResult = Fibonacci(40); // 递归计算斐波那契数列
  36. Console.WriteLine($"斐波那契结果:{fibResult}");
  37. // 2. 数值积分
  38. Console.WriteLine("计算数值积分...");
  39. double integralResult = TrapezoidalRule(0, Math.PI, iterations, Math.Sin);
  40. Console.WriteLine($"积分结果:{integralResult}");
  41. // 3. 素数筛选
  42. Console.WriteLine("筛选素数...");
  43. var primes = SieveOfEratosthenes(iterations); // 返回 int[] 数组
  44. Console.WriteLine($"找到的素数个数:{primes.Length}");
  45. // 4. 矩阵乘法
  46. Console.WriteLine("进行矩阵乘法...");
  47. var matrixResult = MatrixMultiplication(5000);
  48. Console.WriteLine($"矩阵乘法结果第一项:{matrixResult[0, 0]}");
  49. }
  50. // 多核性能测试:使用 Parallel.For 来并行执行任务
  51. static void TestMultiCorePerformance()
  52. {
  53. long iterationsPerCore = 25000000; // 每个核心的计算迭代次数
  54. int coreCount = Environment.ProcessorCount; // 获取当前计算机的核心数
  55. double[] results = new double[coreCount]; // 存储每个核心的计算结果
  56. // 使用 Parallel.For 来并行计算
  57. Parallel.For(0, coreCount, coreIndex =>
  58. {
  59. // 1. 数值积分
  60. double result = TrapezoidalRule(0, Math.PI, 1000000, Math.Sin);
  61. // 2. 素数筛选
  62. var primes = SieveOfEratosthenes(1000000);
  63. // 3. 矩阵乘法
  64. var matrixResult = MatrixMultiplication(500);
  65. result += matrixResult[0, 0];
  66. results[coreIndex] = result; // 存储每个核心的计算结果
  67. });
  68. // 输出每个核心的计算结果(仅作为占位符)
  69. for (int i = 0; i < coreCount; i++)
  70. {
  71. Console.WriteLine($"核心 {i + 1} 的计算结果:{results[i]}");
  72. }
  73. }
  74. // 递归计算斐波那契数列
  75. static long Fibonacci(int n)
  76. {
  77. if (n <= 1)
  78. return n;
  79. return Fibonacci(n - 1) + Fibonacci(n - 2);
  80. }
  81. // 数值积分:梯形法则
  82. static double TrapezoidalRule(double a, double b, int n, Func<double, double> f)
  83. {
  84. double h = (b - a) / n;
  85. double sum = (f(a) + f(b)) / 2.0;
  86. for (int i = 1; i < n; i++)
  87. {
  88. sum += f(a + i * h);
  89. }
  90. return sum * h;
  91. }
  92. // 埃拉托斯特尼筛法:筛选素数
  93. static int[] SieveOfEratosthenes(int limit)
  94. {
  95. // 创建布尔数组,true表示该位置是素数,false表示不是素数
  96. bool[] sieve = new bool[limit + 1];
  97. for (int i = 2; i <= limit; i++)
  98. {
  99. sieve[i] = true;
  100. }
  101. // 筛选出素数
  102. for (int i = 2; i * i <= limit; i++)
  103. {
  104. if (sieve[i])
  105. {
  106. for (int j = i * i; j <= limit; j += i)
  107. {
  108. sieve[j] = false;
  109. }
  110. }
  111. }
  112. // 将布尔数组中的素数转化为整数数组
  113. var primesList = new System.Collections.Generic.List<int>();
  114. for (int i = 2; i <= limit; i++)
  115. {
  116. if (sieve[i])
  117. {
  118. primesList.Add(i);
  119. }
  120. }
  121. return primesList.ToArray(); // 返回素数数组
  122. }
  123. // 矩阵乘法:两个大矩阵的乘法
  124. static double[,] MatrixMultiplication(int size)
  125. {
  126. double[,] matrixA = new double[size, size];
  127. double[,] matrixB = new double[size, size];
  128. double[,] result = new double[size, size];
  129. // 初始化矩阵A和矩阵B
  130. Random rand = new Random();
  131. for (int i = 0; i < size; i++)
  132. {
  133. for (int j = 0; j < size; j++)
  134. {
  135. matrixA[i, j] = rand.NextDouble();
  136. matrixB[i, j] = rand.NextDouble();
  137. }
  138. }
  139. // 矩阵相乘
  140. for (int i = 0; i < size; i++)
  141. {
  142. for (int j = 0; j < size; j++)
  143. {
  144. for (int k = 0; k < size; k++)
  145. {
  146. result[i, j] += matrixA[i, k] * matrixB[k, j];
  147. }
  148. }
  149. }
  150. return result;
  151. }
  152. }