Thread: My 4K Write Program is nowhere near as fast as benchmarks

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    My 4K Write Program is nowhere near as fast as benchmarks

    So I wrote this program that writes 100,000 4K .txt files onto disk.

    HD Tune Pro says my 500 MB RAM disk gets ~90k IOPS for 4k files, ~350 MB/sec

    Here is my program:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdio>
    
    int main() {
        char filename[100];
    
        for(double o = 1; o<100000; o++)
        {
            std::sprintf(filename, "%05.0lf.txt", o);
            std::ofstream ofs(filename);
    
            for(int i = 0; i < 77; i++)
            {
                ofs << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            }
    
            ofs << "aaaaaaaaaaaaaaa";
        }
    
        return 0;
    }
    Which gives me ~400 IOPS on the ramdisk. How can I make this go faster?

    Thanks!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Not exactly sure if the same principle applies in C++, but...

    You should probably make a 4k buffer in the beginning, and initialize it to all 'a's. Then in the loop, copy the entire buffer to the file at once.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Apart from what memcpy says, you cant test it that way because creating so many files means lots of reads and writes to and from the directory. As such, I would expect a program such as the above to take more than twice as long as quoted.
    All you're doing is wearing out the disk faster.
    What would make sense for testing write speed would be to append 4096 bytes to a binary file at a time. You would only have the one file.
    You would use the "write" member function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SPEC CPU Benchmarks
    By Mario F. in forum Tech Board
    Replies: 5
    Last Post: 10-06-2011, 07:45 AM
  2. Not understanding CPU C programming benchmarks...
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-22-2005, 03:49 AM
  3. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 10:10 PM
  4. program closes fast
    By MrSoy in forum C Programming
    Replies: 5
    Last Post: 04-10-2003, 08:00 PM
  5. Need hlep with program output...fast!!
    By Nukesquad in forum C Programming
    Replies: 28
    Last Post: 11-01-2002, 10:00 AM