Quick Q: How can I speed up this nested loop with cout? -- Quora

Quick A: Enable more asynchrony and buffering. Boost can help.

Recently on Quora:

How can I minimize the runtime of the following loops: ... ?

int j=0;
while (j <n) {
    int i = 0 ;
    while (i<m) {
        cout<<" ("<<i<<","<<j<<")"<<endl;
        i++;
    }
    j++;
}

From the first answer:

... That's 16 times faster than the first version.

Add a Comment

Comments are closed.

Comments (1)

0 0

Nyrox said on Apr 28, 2015 07:41 PM:

Use pre-incrementation operator for good measure. Also, if possible, I would buffer into a string and output the result of the whole operation at the end to avoid using the slow 'cout' multiple times.

That are my ideas smile