0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MFCのCMapにてSerialize,Desirialize part.1

Posted at

はじめに

誰も使ってないかも知れませんが、
MFCのCMapでのシリアライズ・デシリアライズを試してみました。
まずは単純なintとCStringのMapで、50万の要素で。

環境

環境は、Visual Studio 2013 Community Edition
MFC アプリ(ダイアログ)です。

コード

名前付けは適当です(笑

MapManager.h
# pragma once
# include <afxtempl.h>

class MapManager
{
private :
        CMap <int , int, CString, CString > m_save_Map;
        CMap <int , int, CString, CString > m_load_Map;

public :
       MapManager();
       ~MapManager();

        void Initialize();
        void Serialize();
        void Deserialize();
};

MapManager.cpp
# include "stdafx.h"
# include "MapManager.h"

MapManager ::MapManager()
{
}


MapManager ::~MapManager()
{
}

void MapManager ::Initialize()
{
      clock_t start, finish;
      double duration;
      start = clock();

      m_save_Map.InitHashTable(500009);// 必要な要素数に一番近い素数がパフォーマンスがいいらしい。
      for (int i = 0; i < 500000; i++)
      {
          CString str;
          str.Format( _T ("item:%d\r\n" ), i);
          m_save_Map[i] = str;
      }
      finish = clock();
      duration = ( double )(finish - start) / CLOCKS_PER_SEC ;
      TRACE (_T ( "map initialized --- %2.3f seconds\n"), duration);
}

void MapManager ::Serialize()
{
      clock_t start, finish;
      double duration;
      start = clock();

      CFile cFileG;
      if (cFileG.Open(_T ( "SampleFile.txt"), CFile ::modeCreate | CFile:: modeWrite ))
      {
          CArchive cArchiveObj(&cFileG, CArchive :: store);
          m_save_Map.Serialize(cArchiveObj);
      }

      finish = clock();
      duration = ( double )(finish - start) / CLOCKS_PER_SEC ;
      TRACE (_T ( "map serialized --- %2.3f seconds\n"), duration);
}

void MapManager ::Deserialize()
{
      clock_t start, finish;
      double duration;
      start = clock();

      CFile cFileG;

      m_load_Map.InitHashTable(500009);
      if (cFileG.Open(_T ( "SampleFile.txt"), CFile ::modeRead ))
      {
          CArchive cArchiveObj(&cFileG, CArchive :: load);
          m_load_Map.Serialize(cArchiveObj);
      }

      finish = clock();
      duration = ( double )(finish - start) / CLOCKS_PER_SEC ;
      TRACE (_T ( "map loaded --- %2.3f seconds\n"), duration);
}

呼び出し側のコードはこんな感じ。(MFCのDialogにて呼び出し)

MapSample.cpp
void CCMapSampleDlg ::OnBnClickedOk()
{
        MapManager * map = new MapManager();
        map->Initialize();
        map->Serialize();
        map->Deserialize();
        delete map;
}

実行結果

速度

  • map initialized --- 1.438 seconds
  • map serialized --- 0.281 seconds
  • map loaded --- 1.122 seconds

SampleFile.txtは、約16MBです。

所感

スピードは悪くないです。

難点は、出力形式が指定できないことですね。文字コード指定できず・・・CFileだとバイナリなので、意味不明な感じになります。
フォーマットも出来ないので・・・。
Image.png

Sakuraで開けば一応改行コード入っているので改行はしてくれますが・・・。

ちなみに、CStdioFileだと、saveは出来ましたが(その際はメモ帳で開いても改行されていました)、loadが出来ませんでした。Debug実行時に以下のエラーが出ました。
Image.png
パラメータの調整が必要なのかも知れません。解析するモチベーションが保てませんでした。

あんまり使えないなぁ。
XML出力という選択肢がある分、Boostのほうがいいと私は思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?