wubing 发表于 2011-7-6 14:48:44

请教一个关于ACE_Mem_Map的使用问题

最近在学习使用ACE_Mem_Map的时候,发现文件写入的效率比较低,居然比fwrite的效率要低,请大家帮忙看看,是否我的用法不对?多谢了!
【代码】main.cpp

#include "ace/OS.h"
#include "ace/Task.h"
#include "ace/Mem_Map.h"
class MyTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
    void mmap()
    {
      char fileName;
      sprintf(fileName, "c:\\mmap_%d.log", ACE_Thread::self());
      const int BUFFLEN = 1*1024;
      char buff;
      memset(buff, '1', BUFFLEN);
      buff = 0;
      time_t t1;
      ACE_OS::time(&t1);
      printf("THR%d:mmap begin...\n", ACE_Thread::self());
      ACE_Mem_Map mmap;
      mmap.map(fileName,
               static_cast<size_t>(100*1024*BUFFLEN),
               O_CREAT|O_RDWR,
               ACE_DEFAULT_FILE_PERMS,
               PROT_RDWR,
               ACE_MAP_SHARED);
      char* tmp = (char*)mmap.addr();
      for(int i=0; i<100*1024; i++)
      {
            memcpy(tmp, buff, BUFFLEN);
            tmp = tmp + BUFFLEN;
      }
      mmap.sync();
      mmap.close();
      time_t t2;
      ACE_OS::time(&t2);
      printf("THR%d:mmap ended, second[%d]!\n", ACE_Thread::self(), t2 - t1);
    }

    void unmmap()
    {
      char fileName;
      sprintf(fileName, "c:\\unmmap_%d.log", ACE_Thread::self());
      const int BUFFLEN = 1*1024;
      char buff;
      memset(buff, '1', BUFFLEN);
      buff = 0;
      time_t t1;
      ACE_OS::time(&t1);
      printf("THR%d:unmmap begin...\n", ACE_Thread::self());
      FILE* fp = ACE_OS::fopen(fileName, "a+b");
      for(int i=0; i<100*1024; i++)
      {
            ACE_OS::fwrite(buff, sizeof(char), BUFFLEN, fp);
      }
      ACE_OS::fclose(fp);
      time_t t2;
      ACE_OS::time(&t2);
      printf("THR%d:unmmap ended, second[%d]!\n", ACE_Thread::self(), t2 - t1);
    }
    int start(int tpNum, bool is_mmap)
    {
      this->is_mmap_ = is_mmap;
      this->activate(THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED, tpNum);
      return 0;
    }
    int svc()
    {
      this->is_mmap_ ? this->mmap() : this->unmmap();
      return 0;
    }
private:
    bool is_mmap_;
};

int main (int argc, char *argv[])
{
    bool is_mmap = false;
    int tpNum = 1;
    for (int i = 1; i<argc; i++)
    {   
      if(memcmp(argv, "-tpNum=", 7) == 0)
      {
            tpNum = atoi(&argv);
      }
      else if(memcmp(argv, "-mmap", 5) == 0)
      {
            is_mmap = true;
      }
    }
    MyTask task;
    task.start(tpNum, is_mmap);
    task.wait();
    return 0;
}

【执行结果】1个并发和10个并发内存映射方式都要慢,并且特别耗CPU
1、内存映射方式
C:\bin>Mem_Map_Test -mmap
THR3052:mmap begin...
THR3052:mmap ended, second!
C:\bin>Mem_Map_Test -mmap -tpNum=10
THR3132:mmap begin...
THR2456:mmap begin...
THR936:mmap begin...
THR3464:mmap begin...
THR3440:mmap begin...
THR3412:mmap begin...
THR3456:mmap begin...
THR3452:mmap begin...
THR3684:mmap begin...
THR3276:mmap begin...
THR3456:mmap ended, second!
THR3452:mmap ended, second!
THR3684:mmap ended, second!
THR3276:mmap ended, second!
THR3132:mmap ended, second!
THR3464:mmap ended, second!
THR936:mmap ended, second!
THR3440:mmap ended, second!
THR3412:mmap ended, second!
THR2456:mmap ended, second!

2、普通方式
C:\bin>Mem_Map_Test
THR3004:unmmap begin...
THR3004:unmmap ended, second!
C:\bin>Mem_Map_Test -tpNum=10
THR2028:unmmap begin...
THR2808:unmmap begin...
THR3224:unmmap begin...
THR3752:unmmap begin...
THR3036:unmmap begin...
THR2304:unmmap begin...
THR1892:unmmap begin...
THR2540:unmmap begin...
THR3736:unmmap begin...
THR2536:unmmap begin...
THR3224:unmmap ended, second!
THR3036:unmmap ended, second!
THR1892:unmmap ended, second!
THR2304:unmmap ended, second!
THR2536:unmmap ended, second!
THR2028:unmmap ended, second!
THR3752:unmmap ended, second!
THR2540:unmmap ended, second!
THR3736:unmmap ended, second!
THR2808:unmmap ended, second!

steven99ca 发表于 2011-7-7 11:09:15

if you only need file IO,read/write are the best!
页: [1]
查看完整版本: 请教一个关于ACE_Mem_Map的使用问题