console output is 'relatively slow' and if you do enough of it redirection to a file is significantly faster (programname > file in both unix and dos will do this). Alternately you can buffer up all the output and drop it all in a single cout statement at the end (just += to a string variable).
Your program is too small to bother with this stuff -- you will barely see the change. If you wrote out thousands of lines, you would notice the difference.
In addition to what jonnin said, note that the use of std::endl will always flush the stream. This usually does not matter for console I/O, because newlines are usually flushed anyway, but using std::endl when outputting to a file can make it much slower if endl is being called thousands of times. https://en.cppreference.com/w/cpp/io/manip/endl