Improve performance of accessing elements in a map and writing to a file

前端 未结 1 1099
一向
一向 2021-01-26 04:11

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

相关标签:
1条回答
  • 2021-01-26 04:29

    On my machine (i7/16gb/win7/vs2013), the example below runs in about:

    • 6s (c stream/binary);
    • 22s (c stream/text);
    • 9s (c++ stream/binary) and
    • 56s (c++ stream/text).

    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;
    }
    
    0 讨论(0)
提交回复
热议问题