Search code examples
c++performanceloopsiostream

C++ Is there any way to make a program run faster?


For example.. if I had.

#include <iostream>
using namespace std;

int main()
{
   int counter = 0;
   while (true)
   {
      cout << counter << endl;
      counter++
   }
}

And say I was on a race to counting to 1 billion against other computers, is the rate at which this loop runs purely dependent on the computer processor speed? Or is there a limit on how fast my program can run, which could be changeable?


Solution

  • Get rid of the endl and use "\n" instead. Plan on at least a 4x speed up from that alone.

    Write the output to a file instead of the screen. That should be good for another 10x speed improvement (or so--more if you use an SSD).