简体   繁体   English

为什么这个C ++程序不使用100%CPU而只使用10%

[英]Why does this C++ program not use 100% CPU but only 10%

This program isn't using all the cpu power. 这个程序没有使用所有的CPU功率。 I was expecting it to take over the cpu and run the code the fastest possible, but it only use 10 max 我期待它能够接管cpu并尽可能快地运行代码,但它最多只能使用10个

#include <iostream>

using namespace std;

int main(void) {
    unsigned long long x = 600851475143;
    unsigned long long i = x-1;

    while(i <= x) {
            cout << "\r";
            cout << i;

            if((x % i) == 0) {
                    cout << "\n\n";
                    cout << i;

                    break;
            }

            i--;
    }

    system("pause");
}

it only goes to a max of 10% 它最多只能达到10%

The speed is probably limited by the speed of the output device. 速度可能受输出设备速度的限制。 If you pipe the output to a file on disk, it'll be limited by the disk speed. 如果将输出通过管道传输到磁盘上的文件,它将受到磁盘速度的限制。 If you just write to a console, it'll be limited by the speed of the console. 如果您只是写入控制台,它将受到控制台速度的限制。 Either way, the CPU will rarely be the limit. 无论哪种方式,CPU很少会成为极限。

Edit: since some people apparently don't quite understand the code, perhaps it's best to simplify it a bit. 编辑:因为有些人显然不太了解代码,也许最好简化一下。 Let's simplify the loop by leaving out the if statement, leaving just: 让我们通过省略if语句来简化循环,只留下:

unsigned long long x = 600851475143;
unsigned long long i = x-1;

while(i <= x) {
        cout << "\r";
        cout << i;
        i--;
}

So, what this does is start from 600851475143 , count down to 0, then stop when i wraps around to std::numeric_limits<unsigned long long>::max() (typically 2 64 -1). 所以,这样做是从600851475143开始,倒数到0,然后当i回到std::numeric_limits<unsigned long long>::max() (通常为2 64 -1)时停止。

Now, if we add the if statement back in, we can mostly ignore the body that it controls, but we do also get a remainder operation happening every iteration. 现在,如果我们重新添加if语句,我们通常可以忽略它控制的主体 ,但我们也会在每次迭代时都进行余数运算。 Without that, the CPU usage would almost certainly be substantially lower still (though the exact number is likely to depend heavily on the hardware -- eg, if you write the output to a disk, the disk's sustained bandwidth will be the controlling factor). 如果没有这个,CPU的使用率几乎肯定会大大降低(尽管确切的数字可能很大程度上取决于硬件 - 例如,如果将输出写入磁盘,磁盘的持续带宽将成为控制因素)。

Because you are making blocking I/O calls via cout. 因为您正在通过cout进行阻止I / O调用。 Remove the cout statements and it will consume more cpu. 删除cout语句,它将消耗更多的CPU。

Whenever a thread is blocked on an I/O operation to complete, including stdout which waits for the console to print, the thread is an blocked state. 每当线程在I / O操作上被阻塞以完成时,包括等待控制台打印的stdout,线程就是阻塞状态。 Hence, no CPU time on that thread until the blocking operation is complete. 因此,在阻塞操作完成之前,该线程上没有CPU时间。 stdout, the kernel, and the console all provide a little bit of buffering to prevent an I/O block, but eventually a program writing fast enough will exceed the buffering provided. stdout,内核和控制台都提供了一点缓冲以防止I / O块,但最终编程速度足够快的程序将超过提供的缓冲。

Note. 注意。 On Windows, this program (with the cout print statements removed) will only use as a maximum of one core of CPU. 在Windows上,此程序(删除了cout打印语句)将仅用作CPU的一个核心。 If you are on a quad core, it will only consume 25% of CPU as reported on Windows task manager. 如果您使用四核,它将仅消耗Windows任务管理器上报告的CPU的25%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM