首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么CFile + CArchive比C/O流表现更好?

为什么CFile + CArchive比C/O流表现更好?
EN

Stack Overflow用户
提问于 2019-07-08 00:06:03
回答 1查看 286关注 0票数 2

我正在比较MFC类之间的性能:

  • CFile + CArchive (使用缓冲区)

  • CStdioFile (使用缓冲I/O流)

两者都使用一个缓冲器。

以下是我的代码:

代码语言:javascript
复制
void TestFun1(CFile& File, BOOL bIsMemFile)
{
    CArchive Archive(&File, CArchive::store);
    CString strLine, strOutput;
    UINT uSize;
    BYTE* lpBuf;

    ULONGLONG uStart, uStop;

    uStart = ::GetTickCount64();

    for (UINT nIndex = 0; nIndex < 500; nIndex ++)
    {
        //  Reset the file to empty
        File.SetLength(0);

        strLine.Format(_T("This is line %u."), nIndex);

        for (UINT j = 0; j < 5000; j++)
            File.Write((LPCTSTR)strLine, strLine.GetLength() * sizeof(TCHAR));

        File.Flush();
    }

    uStop = ::GetTickCount64();

    CString strMsg;

    strMsg.Format(_T("Total time(TestFun1): %I64u."), uStop - uStart);
    AfxMessageBox(strMsg);
}

void TestFun2(CFile& File, BOOL bIsMemFile)
{
    CArchive Archive(&File, CArchive::store);
    CString strLine, strOutput;
    UINT uSize;
    BYTE* lpBuf;

    ULONGLONG uStart, uStop;

    uStart = ::GetTickCount64();

    for (UINT nIndex = 0; nIndex < 500; nIndex ++)
    {
        //  Reset the file to empty
        File.SetLength(0);

        strLine.Format(_T("This is line %u."), nIndex);

        for (UINT j = 0; j < 5000; j++)
            Archive.WriteString(strLine);

        Archive.Flush();
    }

    uStop = ::GetTickCount64();

    CString strMsg;

    strMsg.Format(_T("Total time(TestFun2): %I64u."), uStop - uStart);
    AfxMessageBox(strMsg);
}

void CTestMemFileDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here
    CFile File;
    CMemFile MemFile;

    if (File.Open(_T("E:\\Temp\\testfile.dat"), CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone))
    {
        TestFun1(File, FALSE);
        TestFun2(File, FALSE);
        TestFun1(File, FALSE);
        TestFun2(File, FALSE);

        File.Close();
    }
}

void CTestMemFileDlg::OnBnClickedButton2()
{
    // TODO: Add your control notification handler code here
    CStdioFile File;
    CMemFile MemFile;

    if (File.Open(_T("E:\\Temp\\testfile.dat"), CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone))
    {
        int nVal;

        nVal = setvbuf(File.m_pStream, NULL, _IOFBF, 1024768);

        TestFun1(File, FALSE);
        TestFun2(File, FALSE);
        TestFun1(File, FALSE);
        TestFun2(File, FALSE);

        File.Close();
    }
}

研究结果如下:

Button1:

代码语言:javascript
复制
TestFun1: 18174
TestFun2: 375
TestFun1: 18330
TestFun2: 375

Button2:

代码语言:javascript
复制
TestFun1: 546
TestFun2: 530
TestFun1: 530
TestFun2: 531

根据我的测试,CFile + CArchive总是占用CStdioFile的66%的时间。我试图将缓冲区增加到CStdioFile,使其高达1MB,但仍然得到相同的结果。

因此,我的问题是,既然这两种解决方案都使用缓冲,为什么CStdioFile总是比CFile + CArchive慢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-08 01:15:02

默认情况下,CStdioFile以文本模式打开文件。这将导致特殊字符的转换,例如回车和行提要,这显然需要时间。

如果要避免这种情况,可以将CFile::typeBinary添加到CStdioFile::open()调用中的nOpenFlags参数中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56927022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档