0

I'm new to programming and I just learned and am playing around with fstream and noticed the writing speed is approximately 300KBps. Here is my basic program:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int countX = 0;
    int countY = 0;

    ofstream output;

    output.open("output.txt");

    for (int i = 0; i < 1000; i++)
    {
        for (int y = 0; y < 1000; y++)
        {
            countX++;

            output << 1; //Record output.txt 1 byte of information
                         //300KBps
        }
    }
    output << endl << countX;
    output.close();
    return 0;
}

Now I'm wondering if there is a way to speed this process? (Except the obvious better hardware)E.g. Devoting more processing power to program. My computer utilization shows about 10-14%.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • try buffering output, like `char[1000] c;for(int j=0;j<1000;j++)c[j]=1;output << c;` – ankhzet Sep 12 '15 at 20:09
  • What makes you actually think that `1` comprises _one byte of information_? These assumptions are totally wrong. – πάντα ῥεῖ Sep 12 '15 at 20:09
  • You can generally expect to gain I/O performance if you do it in larger batches. That is, write out a whole character array `"11111111…"` instead of a single byte at a time. It will also avoid formatting the integer each time. But it is hard to give you better advice without knowing what you're actually trying to do. You don't really want megabytes of `1`s written to a file, do you? – 5gon12eder Sep 12 '15 at 20:10
  • @πάνταῥεῖ I don't really see your point. Do you want to nitpick on the use of the word *information* because it is defined as the entropy and the large number of `1`s has much less than eight bits of entropy per byte? As far as I/O is concerned, I don't think that the assumption that the integer 1, which will be formatted to the single character `1`, will cause a single byte of output is “totally wrong”. – 5gon12eder Sep 12 '15 at 20:15
  • @5gon12eder _", which will be formatted ..."_ is my nitpick! And lack of showing exact measurements in the question of course. – πάντα ῥεῖ Sep 12 '15 at 20:17
  • Hey guys, thank you so much for your help! I am trying out these suggestions and will leave an update! @πάντα ῥεῖ The text file produced is approximately 1MB. 1MB divided by 1,000,000 million 1's = 1 byte. Oh and aren't each characters made up of 8 bits? Which being 8 bits = 1 byte? I'm not building a rocketship here dude, just trying to learn how to program more efficiently. –  Sep 12 '15 at 20:26
  • @VardanBetikyan You've been missing to take the time needed for formatting into account, that was all of my point, why your measures are interpreted wrong. – πάντα ῥεῖ Sep 12 '15 at 20:30
  • all of you forgetting, that disk operations is _always_ buffered, and _always_ goes in blocks of device's throughput bus. Thus, even if you send `1` (one) byte of informattion, the hardware will still transferr block of bytes there and here, just actual meaning will have only one byte of it. – ankhzet Sep 12 '15 at 20:42
  • So, best writing speed _ever_ can only be reachible, if make writing operations with blocks of size near the device i/o capability. see at hdd's rpm rates for that info – ankhzet Sep 12 '15 at 20:43
  • I tried the array method but it brings out to the same speed. Basically I'm just trying to simulate the 1's "byte" as data being created and saved. Example: Using PrtSc = new data -> compress and transfer into text data -> save into .txt. –  Sep 12 '15 at 20:50
  • So when I execute this program, basically, it uses its max speed possible? Can't I somehow increase the utilization used for this program specifically? –  Sep 12 '15 at 20:52
  • No, it uses buffering capabilities of used software library. To achieve max possible speed, you need to make i/o at lowest possible abstraction layer... and that quite hard for newbies =\ – ankhzet Sep 12 '15 at 20:54
  • You can look [here](http://stackoverflow.com/questions/11563963/writing-a-binary-file-in-c-very-fast) for possible tips&hints – ankhzet Sep 12 '15 at 20:57
  • 1
    @ankhzet. Maybe you misunderstand blocks and buffering and streams? Yes, HDD can only read or write entire sectors, but writing one byte to an ofstream doesn't cause any transfer to the disk, until the I/O library's buffer is full, or the file is closed. – Roddy Sep 12 '15 at 21:18
  • there is such an operation, called `flush`... depends on what i/o api you using, flush operations can be performed for buffer sizes, much less than device throughput capability. So, even for 1-byte write operation there can be called unefficient flush operation witch will requre entire sector reading/writing. To get rid of this possibility - use as low level api, as possible. That was my point. – ankhzet Sep 12 '15 at 21:25

0 Answers0