I have an large array in memory. I am writing this in file using:
FILE* fp = fopen("filename", "wb");
fwrite(array, sizeof(uint32_t), 1500000000 , fp); // array saved
fflush(fp) ;
fclose(fp);
and reading it again using:
FILE* fp = fopen("filename", "rb");
fread(array, sizeof(uint32_t), 1500000000 , fp);
fclose(fp);
For, writing it takes 7 sec and for reading it takes 5 sec.
Actually, I have not to write whole array. I have to write and read it by checking some conditions. Like (example case):
#include<iostream>
#include <stdint.h>
#include <cstdio>
#include <cstdlib>
#include <sstream>
using namespace std;
main()
{
uint32_t* ele = new uint32_t [100] ;
for(int i = 0; i < 100 ; i++ )
ele[i] = i ;
for(int i = 0; i < 100 ; i++ ){
if(ele[i] < 20)
continue ;
else
// write ele[i] to file
;
}
for(int i = 0; i < 100 ; i++ ){
if(ele[i] < 20)
continue ;
else
// read number from file
// ele[i] = number * 10 ;
;
}
std::cin.get();
}
For this reason what I am doing is:
writing using:
for(int i = 0; i < 1500000000 ; i++ ){
if (arrays[i] < 10000000)
continue ;
uint32_t number = arrays[i] ;
fwrite(&number, sizeof(uint32_t), 1, fp1);
}
And reading using: fread(&number, sizeof(uint32_t), 1, fp1);
This case: writing takes 2.13 min and for reading it takes 1.05 min.
Which is quite long time for me. Can anybody help me, why is this happening (in second case file size is less than first one) ? And How to solve this issue ? Any other better approach ?