找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5833|回复: 1

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

[复制链接]
发表于 2011-7-6 14:48:44 | 显示全部楼层 |阅读模式
最近在学习使用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[128];
        sprintf(fileName, "c:\\mmap_%d.log", ACE_Thread::self());
        const int BUFFLEN = 1*1024;
        char buff[BUFFLEN+1];
        memset(buff, '1', BUFFLEN);
        buff[BUFFLEN] = 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[128];
        sprintf(fileName, "c:\\unmmap_%d.log", ACE_Thread::self());
        const int BUFFLEN = 1*1024;
        char buff[BUFFLEN+1];
        memset(buff, '1', BUFFLEN);
        buff[BUFFLEN] = 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[i], "-tpNum=", 7) == 0)
        {
            tpNum = atoi(&argv[i][7]);
        }
        else if(memcmp(argv[i], "-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[10]!
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[193]!
THR3452:mmap ended, second[212]!
THR3684:mmap ended, second[212]!
THR3276:mmap ended, second[217]!
THR3132:mmap ended, second[218]!
THR3464:mmap ended, second[218]!
THR936:mmap ended, second[219]!
THR3440:mmap ended, second[221]!
THR3412:mmap ended, second[222]!
THR2456:mmap ended, second[224]!

2、普通方式
C:\bin>Mem_Map_Test
THR3004:unmmap begin...
THR3004:unmmap ended, second[4]!
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[33]!
THR3036:unmmap ended, second[36]!
THR1892:unmmap ended, second[38]!
THR2304:unmmap ended, second[39]!
THR2536:unmmap ended, second[40]!
THR2028:unmmap ended, second[40]!
THR3752:unmmap ended, second[40]!
THR2540:unmmap ended, second[40]!
THR3736:unmmap ended, second[41]!
THR2808:unmmap ended, second[41]!
发表于 2011-7-7 11:09:15 | 显示全部楼层
if you only need file IO,read/write are the best!
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-11-21 23:21 , Processed in 0.014407 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表