I have a map structure in C++ which holds about 60 million pairs. After I populate the map, I would like to write it to a plain file.
At the moment this code is quite
On my machine (i7/16gb/win7/vs2013), the example below runs in about:
On a different machine (PentiumD/4gb/win10/vs2015), the longest time was about 4 minutes.
#include <iostream>
#include <cstdint>
#include <map>
#include <chrono>
int main()
{
typedef std::pair<uint32_t, uint32_t> C_Value;
typedef std::map< uint32_t, C_Value > C_Map;
//
C_Map m;
const uint32_t maxsize = 50000000;
try
{
for ( int i = 0; i < maxsize; ++i )
m[i] = C_Value( i, i );
}
catch ( ... )
{
std::cout << "too many elements\n";
return -1;
}
//
std::cout << "writing " << m.size() << " elements... ";
auto t_start = std::chrono::high_resolution_clock::now();
#if 1
//
FILE* f = fopen( "test.bin", "wb" );
if ( ! f )
{
std::cout << "could not open file\n";
return -2;
}
//
for ( auto e : m )
{
fwrite( &e.second.first, sizeof e.second.first, 1, f );
fwrite( &e.second.second, sizeof e.second.second, 1, f );
}
//
fclose( f );
#endif
#if 0
//
FILE* f = fopen( "test.bin", "w" );
if ( ! f )
{
std::cout << "could not open file\n";
return -2;
}
//
for ( auto e : m )
{
fprintf( f, "%d, %d\n", e.second.first, e.second.second );
}
//
fclose( f );
#endif
#if 0
std::ofstream os( "test.bin", std::ios::binary );
if ( ! os )
{
std::cout << "could not open file\n";
return -2;
}
//
for ( auto e : m )
{
os.write( (const char*)&e.second.first, sizeof e.second.first );
os.write( (const char*)&e.second.second, sizeof e.second.second );
}
//
os.close();
#endif
#if 0
std::ofstream os( "test.bin" );
if ( ! os )
{
std::cout << "could not open file\n";
return -2;
}
//
for ( auto e : m )
{
os << e.second.first << ',' << e.second.second << '\n';
}
//
os.close();
#endif
//
auto t_end = std::chrono::high_resolution_clock::now();
std::cout << "done in [ms]: " << std::chrono::duration<double, std::milli>( t_end-t_start ).count() << std::endl;
return 0;
}