Rah*_*han 5 .net c# optimization performance multithreading
我知道设置线程优先级是堆栈溢出的一个禁忌话题,但我确信我的应用程序是提高优先级的好候选人。为了证明这一点,我在下面解释了上下文。现在的问题是如何有效地做到这一点?
该应用程序是 .NET 4 (C#) 控制台应用程序,它执行复杂的算法,执行时间约为 5 小时。该算法根本不是内存密集型的,只是处理器密集型的。它执行数字运算,不执行任何磁盘 I/O、数据库连接、网络连接等。应用程序的输出只是一个最后写入控制台的数字。换句话说,该算法是完全自包含的,没有任何依赖关系。
该应用程序在其自己的专用 16 核 64 位机器上运行,该机器运行 Windows Server,其可用 RAM 远远超过其所需 (8GB)。专用我的意思是已购买服务器以独家运行此应用程序。
我已经通过广泛的分析、花哨的数学快捷方式和一些小技巧对代码进行了尽可能多的优化。
以下是伪代码的整体结构:
public static void Main ()
{
Process.GetCurrentProcess().PriorityBoostEnabled = true;
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
// Of course this only affects the main thread rather than child threads.
Thread.CurrentThread.Priority = ThreadPriority.Highest;
BigInteger seed = SomeExtremelyLargeNumber; // Millions of digits.
// The following loop takes [seed] and processes some numbers.
result1 = Parallel.For(/* With thread-static variables. */);
while (true) // Main loop that cannot be parallelized.
{
// Processes result1.
result2 = Parallel.For(/* With thread-static variables. */);
// Processes result2.
result1 = Parallel.For(/* With thread-static variables. */);
if (result1 == criteria)
break;
// Note: This loop does not need to sleep or care about system responsiveness.
}
}
Run Code Online (Sandbox Code Playgroud)
现在,基于 SO 上与线程优先级相关的问题,我认为任何使用 ThreadPool 的东西都不应该在优先级方面被混淆。所以如果我需要切换到手动线程,就这样吧。
题:
对于这样的应用程序,我希望更改优先级以使整体运行时间的差异为 0%。如果您的 CPU 使用率已经达到极限,所有 16 个核心都 100% 地执行实际工作,那么您就无能为力了。