windows 增加 C++ 程序 CPU 使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6449332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:08:55  来源:igfitidea点击:

Increasing C++ Program CPU Use

c++windowsperformancememory-managementcpu

提问by TimeCoder

I have a program written in C++ that runs a number of for loops per second without using anything that would make it wait for any reason. It consistently uses 2-10% of the CPU. Is there any way to force it to use more of the CPU and do a greater number of calculations without making the program more complex? Additionally, I compile with C::B on a Windows computer. Essentially, I'm asking whether there is a way to make my program faster by increasing usage of CPU, and if so, how.

我有一个用 C++ 编写的程序,它每秒运行多个 for 循环,而没有使用任何会使其因任何原因等待的东西。它始终使用 2-10% 的 CPU。有没有办法强制它使用更多的 CPU 并进行更多的计算而不会使程序更复杂?此外,我在 Windows 计算机上使用 C::B 进行编译。本质上,我在问是否有办法通过增加 CPU 使用率来使我的程序更快,如果是,如何。

采纳答案by paxdiablo

Assuming your application has the power (PROCESS_SET_INFORMATIONaccess right), you can use SetPriorityClassto bump up your priortiy (to the usual detriment of all other processes, of course).

假设您的应用程序具有权限(PROCESS_SET_INFORMATION访问权限),您可以使用它SetPriorityClass来提高您的优先级(当然,这通常会损害所有其他进程)。

You can go ABOVE_NORMAL_PRIORITY_CLASS(try this one first), HIGH_PRIORITY_CLASS(be very careful with this one) or REALTIME_PRIORITY_CLASS(I would strongly suggest that you probably shouldn'tgive this one a shot).

你可以去ABOVE_NORMAL_PRIORITY_CLASS(先试试这个),HIGH_PRIORITY_CLASS(对这个要非常小心)或REALTIME_PRIORITY_CLASS(我强烈建议你可能不应该尝试这个)。

If you try the higher priorities and it's still clocking pretty low, then that's probably because you're not CPU-bound (such as if you're writing data to an output file). If that's the case, you'll probably have to find a way to makeyourself CPU bound.

如果您尝试更高的优先级并且它的时钟仍然很低,那么这可能是因为您不受 CPU 限制(例如,如果您将数据写入输出文件)。如果是这种情况,您可能必须找到一种方法自己受 CPU 限制。

Just keep in mind that doing so may not be necessary (or even desirable). If you're running at a higher priority than other threads and you're still not sucking up a lot of CPU, it's probably because Windows has (most likely, rightfully) decided you don't need it.

请记住,这样做可能没有必要(甚至不需要)。如果您以比其他线程更高的优先级运行并且您仍然没有占用大量 CPU,则可能是因为 Windows 已经(最有可能,理所当然地)决定您不需要它。

回答by Billy ONeal

That depends on why it's only using 10% of the CPU. If it's because you're using a multi-CPU machine and your program is using only one CPU, then no, you will have to introduce concurrency into your code to use that additional horsepower.

这取决于为什么它只使用 10% 的 CPU。如果这是因为您使用的是多 CPU 机器并且您的程序只使用一个 CPU,那么不,您必须在代码中引入并发性以使用额外的马力。

If it's being limited by something else (e.g. copying data to and from the disk), then you don't need to focus on CPU, you need to focus on whatever the bottleneck is. Most likely, the limiter will be reading from the disk, which you can improve by using better caching mechanisms.

如果它受到其他东西的限制(例如将数据复制到磁盘和从磁盘复制数据),那么您不需要关注 CPU,您需要关注瓶颈是什么。最有可能的是,限制器将从磁盘读取,您可以通过使用更好的缓存机制来改进。

回答by ssube

It's really not the program's right or responsibility to demand additional resources from the system. That's the OS' job, as resource scheduler.

从系统中要求额外资源实际上不是程序的权利或责任。这是操作系统的工作,作为资源调度程序。

If it is necessary to use more CPU time than the OS sees fit, you should request that from the OS using the platform-dependent API. In this case, that seems to be something along the lines of SetPriorityClassor SetThreadPriority.

如果需要使用比操作系统认为合适的更多 CPU 时间,您应该使用平台相关的 API 从操作系统请求它。在这种情况下,这似乎是沿着线的东西SetPriorityClassSetThreadPriority

回答by Alok Save

Creating a thread & giving higher priority to the thread might be one way.

创建线程并为线程提供更高的优先级可能是一种方法。

回答by prosseek

If you use C++, consider using Intel Threading Building Block. You can find some examples here.

如果您使用 C++,请考虑使用英特尔线程构建块。您可以在此处找到一些示例。

回答by Tom

Some profilers give very nice indications of where bottlenecks in your code are. For example - the CodeAnalyst (for AMD chips only) has the instructions per cycle ratio. I'm sure intel profilers are similar.

一些分析器非常好地指出了代码中的瓶颈所在。例如 - CodeAnalyst(仅适用于 AMD 芯片)具有每周期比的指令。我确信英特尔分析器是相似的。

As Billy O'Neal says though, if your runnning on an 8-core, being stuck on 10 percent of cpu is about right. If this is your problem then Windows msvc++ has a parallel mode (the parallel patterns library) for the standard algorithms. This can give parallelisation for free if have written your loops the c++ way (its still your responsibility to make sure your loops are thread safe). I've not used the msvc version but the gnu::__parallel_for_eachetc work a treat.

正如比利奥尼尔所说,如果你在 8 核上运行,卡在 10% 的 CPU 上是正确的。如果这是您的问题,那么 Windows msvc++ 具有用于标准算法的并行模式(并行模式库)。如果以 C++ 方式编写循环,这可以免费提供并行化(确保循环是线程安全的仍然是您的责任)。我没有使用过 msvc 版本,但gnu::__parallel_for_each等工作很好。