Fastest way to write binary files

Asked 2 years ago, Updated 2 years ago, 117 views

You need to write about 80GB of data on an SSD. My code is about 20MB/s when I turned it on Windows 7 and Visual 2010 As far as I know, file transfer between SSDs is possible from 150 to 200 MB, so I think my code is too inefficient.

In theory, I think there's a way to write seven times faster, so please tell me a better source code!

#include <fstream>
using namespace std;
const unsigned long long size = 64ULL*1024ULL*1024ULL;
unsigned long long a[size];
int main()
{
    fstream myfile;
    myfile = fstream("file.binary", ios::out | ios::binary);
    //Here would be some error handling
    for(int i = 0; i < 32; ++i){
        //Some calculations to fill a[]
        myfile.write((char*)&a,size*sizeof(unsigned long long));
    }
    myfile.close();
}

performance c++ io file-io

2022-09-22 14:10

1 Answers

When I experimented on my computer (SSD) It took about 36 seconds (approximately 220 MB/s) to use all 8GB. I think I used it as much as I could on an SSD.

#include <stdio.h>
const unsigned long long size = 8ULL*1024ULL*1024ULL;
unsigned long long a[size];

int main()
{
    FILE* pFile;
    pFile = fopen("file.binary", "wb");
    for (unsigned long long j = 0; j < 1024; ++j){
        //Some calculations to fill a[]
        fwrite(a, 1, size*sizeof(unsigned long long), pFile);
    }
    fclose(pFile);
    return 0;
}


2022-09-22 14:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.