klenin / Spawner

Cross-platform sandbox runner
4 stars 8 forks source link

Check out idleness test #7

Open MultiTeemer opened 9 years ago

MultiTeemer commented 9 years ago

Why cout << "a" in while(true) cycle doesn't cause idle time limit?

ShigiDono commented 9 years ago

It appears that running this program under spawner consumes too much CPU. Making program to output in blocks like

char buffer[4096];
for (int i = 0; i < 4096; ++i) {
    buffer[i] = 'a';
}
buffer[4095] = 0;
    while (true) {
std::cout << buffer;
}

appears to be eating less CPU, but nevertheless it is TOO much. Assuming that running program does need time to output data to console and in case of output directly to pipe it doesn't, we may conclude that the reason is in this near-zero cost IO.

As proof of this theory, if you run program with enabled output to std::output you will see dramatic fall in CPU usage down to 0. Of course, I may be wrong.

Anyway, I'll check if there is any way to counter this.