Question

For whatever reason I decided to make a very simple application that displays all the numbers from 1-1,000,000. I noticed that it takes way longer to display the results than it does to store them. I assumed that it would take longer to store all the values than it would to display them. I also remember reading, if I remember right, that there isn't really any difference between user input and reading from a text file in a console application, so I assumed that display and storage are the same. Can someone explain why this is? According to the book I'm reading ofstream is really similar to cout, so I dont' understand why it takes longer.

Why is it slower to display the results using cout than it is to store the results in a text file using ofstream?

cout = 169.168 seconds

ofstream = 3.473 seconds

ofstream method

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

namespace patch //used to patch some C++11 functions not included in mingw 4.7 // to_string
{
    template < typename T > std::string to_string( const T& n )
    {
        std::ostringstream stm ;
        stm << n ;
        return stm.str() ;
    }
}
using namespace patch;
using namespace std;

int main()
{
    int minimumRange = 1; //sets minimum number in range
    int maximumRange = 1000000; // sets maximum number in range
    string strRanges = to_string(minimumRange) + "-" + to_string(maximumRange); //appends string names
    ofstream myRange;
    myRange.open(strRanges + ".txt");

    for (int i = minimumRange; i<=maximumRange; ++i)
    {
        myRange << to_string(i) << "\n";
    }
   myRange.close();
    return 0;
}

cout method

#include <iostream>
using namespace std;

int main()
{
    int minimumRange = 1;
    int maximumRange = 1000000;
    for (int i = minimumRange; i<=maximumRange; ++i)
    {
        cout << i << "\n"; //using "/n" instead of endl; for speed purposes
    }
    return 0;
}
Was it helpful?

Solution

On many systems, cout is just a ostream instance mapping the CON file.

The point is that ostream doesn't write anything.

Its purpose is to convert whatever supported type (maninly: const char*, std::string, int, long..., double ...) into a "sequence of characters" to be placed in a buffer (see std::streambuf).

In this sense storing or typing does the same.

The difference arises when the buffer characters have to be spit out towards the physical device: That's where the device drivers and related physical problems (essentially: bandwidth, latency and interleaving) come into play.

When the OS trasnfer a bulk of data to a disk file, it uses a driver that may internally use caching that talks to an external device using a communication protocol (mainly SATA, SCSI, NFS...) that talks toward a controller (a microcomputer on the disk) tha also has its caching that later manipulates the magnetic storage.

Your program says "write", the OS tells "written" but data are still being worked out by device controllers outsite of the OS control. The "slow" part happens asyncronously respect to you.

When you have to write to the console, most of this "caching" cannot be done: as soon you wait for an input, cout is flushed (you want to read what you wrote if it is a question you have to answer).

For this reason the console driver has to wait for the console program to complete its writing before proceed over. But it's writing is in fact a "painting": the characters have to be converted into pixels by means of fonts, and painting must be in sync with the display scanning (otherwise the image will be "disturbed").

This process is slower (an longer) than "writing to disk" that -in fact- translates into "move a bulk of memory from the process to an IO port" (and where the hardware can really do this by swapping two pointer registers)

Also, the console program is optimized for data to be seen (and makes no sense to make it faster than the eyes), while a disk driver is optimized for data to be transfered. It's quite normal for it to be faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top