有没有办法限制 c++ 应用程序使用的 CPU 数量

程序Raj

我正在开发一个计算 recaman 序列的程序。我想计算然后可视化序列中的数千或数百万个术语。但是,我注意到它占用了 10% 的 CPU,并且任务管理器说它的耗电量非常高。我不想损坏电脑,也愿意为了电脑的安全而牺牲速度。有没有办法限制这个应用程序的 CPU 使用率或电池消耗水平?

这适用于 Windows 10。

//My Function for calculating the sequence
//If it helps, you could look up 'Recaman Sequence' on google

void processSequence(int numberOfTerms) {
    int* terms;
    terms = new int[numberOfTerms];

    terms[0] = 0;
    cout << "Term Number " << 0 << " is: " << 0 << endl;

    int currentTermNumber = 1;
    int lastTerm = 0;
    int largestTerm = 0;

    for (currentTermNumber; currentTermNumber < numberOfTerms; currentTermNumber++) {
        int thisTerm;
        bool termTaken = false;
        for (int j = 0; j < numberOfTerms; j++) {
            if (terms[j] == lastTerm - currentTermNumber) {
                termTaken = true;
            }
        }

        if (!termTaken && lastTerm - currentTermNumber > 0) {
            thisTerm = lastTerm - currentTermNumber;
        }
        else {
            thisTerm = lastTerm + currentTermNumber;
        }

        if (thisTerm > largestTerm) {
            largestTerm = thisTerm;
        }
        lastTerm = thisTerm;

        cout << "Term Number " << currentTermNumber << " is: " << thisTerm << endl;
    };

    cout << "The Largest Term Number Was: " << largestTerm << endl;

    delete[] terms;
}
昆汀

使用较少 CPU 的最简单方法是不时地休眠一小段时间,例如一或几毫秒。您可以通过调用 Sleep (Windows API) 或 current_thread::sleep (自 C++11 以来的标准)来实现。

然而,

  • 在 100% 使用所有内核时,您永远不会物理损坏您的计算机。无论如何,大多数电子游戏都是贪婪的。可能发生的最坏情况是突然关闭并且在接下来的几分钟内无法再次打开,以防 CPU 达到极限温度 (80-100°C)。这种安全性确实可以防止任何危险和/或不可恢复的事情发生。
  • 像这样故意减慢程序的速度几乎没有意义。如果您遇到用户界面缓慢的问题,您应该将密集处理移至非 UI 线程。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法限制Bazel使用的CPU内核数量?

有没有办法限制文件处理程序实例的数量?

有没有办法从C应用程序与驱动程序进行通信?

在 C# windows 窗体应用程序中,有没有办法使用整数访问按钮?

有没有办法在C#桌面应用程序中使用oauth2登录Xero api?

有没有办法限制总体CPU消耗?

有没有办法在JavaScript中使用C ++?

将Java应用程序的CPU使用率限制为特定数量

有没有办法确定我的系统拥有和使用的PCIe通道数量

有没有办法在C ++中创建未知数量的变量的构造函数?

有没有办法使用 Android 应用程序捕获云 Firestore 上的任何更新,然后在 C# 应用程序中触发监听事件?

有没有办法使用python列出json中未知数量的元素?

有没有办法使用 DBSCAN 分配最大数量的集群?

在 R 中有没有办法使用 dplyr 包来采样相同数量的因子?

有没有办法限制由使用“与父母一起提交”提交的 Gerrit 合并事件触发的 Jenkins 构建数量?

有没有办法将某种安装程序内置到 C# 应用程序本身中?

有没有办法刷新与程序相关的整个CPU缓存?

有没有办法增加赋予 python 程序的 CPU 能力?(Python 3.5,Mac)

有没有办法获得给定数量的输入,而输入的数量是由模板在c ++中的编译时给出的?

有没有办法检查 sycl 使用的 CPU 线程数?

有没有办法在JVM或Dalvik运行时将CPU使用率限制为一定百分比?

有没有办法自动对模型应用限制?

有没有办法限制特定应用程序的上载速率?

有没有办法限制基于IP的应用程序?

有没有办法限制我的应用程序中GCD产生的线程数?

有没有办法使用 C++ 计算程序画布的大小?

有没有办法以类似于 Python 的方式在 c# 中设置列表项数量?

有没有办法增加Swift中double类型的数量限制?

有没有办法限制来自youtube api的查询数量?