Possible Duplicate:
Writing a binary file in C++ very fast
I have a large number of unsigned 32 bit integers in memory (1.5 billion entries). I need to write them to a file and read them back into main memory.
Now, I do it using:
ofstream ofs;
ofs.open(filename);
for (uint64_t i = 0 ; i < 1470000000 ; i++)
ofs << integers << " " ;
and
ifstream ifs;
ifs.open(filename);
for (uint64_t i = 0 ; i < 1470000000 ; i++)
ifs >> integers ;
This takes a few minutes to execute. Can anybody help me, is there any library method to do it in a faster way? Or any suggestion, so I can run a performance test? Can anybody show me some simple C++ code that uses mmap
for doing the above (on Linux)?
EDIT: 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();
}