目前正在学习C++,想用C++的文件流代替MFC的CFile来读取位图,结果发现在VC6中文件流的表现太差,要300多ms读取一幅768*576*3的位图(P4,Release版),而CFile只要3ms左右,差了一百倍啊!!!本来以为是自己的程序写得不对,后用VC7.1转了工程测试了一番,发现文件流的要7ms左右,CFile的不变,虽然还是差了2倍,但是可以接受了。VC6和VC7在C++方面真的差了这么远???有没有人比较过string和CString的执行效率?各位使用C++的前辈用的什么编译器?与用户界面相关的编码用的什么编译器?用VC6的话,是否还会尽量使用C++呢?我所在的公司还在用VC6,所以我不能用VC7来做项目。读取文件差了这么远,估计我是不能用C++的文件流了,其他的(如string,vector等)没测过还不知道。附源代码(代码中如有不足,敬请提出。输出结果为100次读取的总时间):#define WIDTHBYTES(biWidth, biBitCount) ((((biWidth * biBitCount) + 31) & ~31) >> 3)struct _tagImage
{
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
RGBQUAD quad[256];
BYTE* data;
};_tagImage g_Image;bool ReadBMPFile_cpp(LPCSTR lpszFileName);
bool ReadBMPFile_mfc(LPCSTR lpszFileName);int main(int argc, char* argv[])
{
memset(&g_Image, 0, sizeof(g_Image));
g_Image.data = new BYTE[768 * 576 * 3]; ReadBMPFile_cpp("G:\\ff591.BMP");
ReadBMPFile_mfc("G:\\ff591.BMP"); delete [] g_Image.data; return 0;
}bool ReadBMPFile_cpp(LPCSTR lpszFileName)
{
DWORD dwTick = GetTickCount();
for (int i = 0; i < 100; i++)
{
ifstream fin(lpszFileName, ios::in | ios::binary);
if (!fin)
{
ASSERT(false);
return false;
}

fin.read(reinterpret_cast<char*>(&g_Image.bf), sizeof(g_Image.bf));
fin.read(reinterpret_cast<char*>(&g_Image.bi), sizeof(g_Image.bi));

// 读取调色板
BITMAPINFOHEADER* pBIH = &(g_Image.bi);
if (pBIH->biBitCount != 24)
{
int nSizeTable = (1 << pBIH->biBitCount) * sizeof(RGBQUAD);
fin.read(reinterpret_cast<char*>(g_Image.quad), nSizeTable);
}

DWORD dwDataSize = WIDTHBYTES(pBIH->biWidth, pBIH->biBitCount) * labs(pBIH->biHeight);

fin.seekg(g_Image.bf.bfOffBits, ios::beg);
fin.read(reinterpret_cast<char*>(g_Image.data), dwDataSize);

fin.close(); cout << ".";
} cout << endl;
cout << "CPP: " << GetTickCount() - dwTick << " ms" << endl; return true;
}bool ReadBMPFile_mfc(LPCSTR lpszFileName)
{
DWORD dwTick = GetTickCount(); for (int i = 0; i < 100; i++)
{
CFile file;
if (!file.Open(lpszFileName, CFile::modeRead))
{
ASSERT(false);
return false;
}

file.Read(&g_Image.bf, sizeof(g_Image.bf));
file.Read(&g_Image.bi, sizeof(g_Image.bi));

if (g_Image.bf.bfType != 0x4d42)
{
file.Close();
ASSERT(false);
return false;
}

// 读取调色板
BITMAPINFOHEADER* pBIH = &(g_Image.bi);
if (pBIH->biBitCount != 24)
{
int nSizeTable = (1 << pBIH->biBitCount) * sizeof(RGBQUAD);
file.Read(g_Image.quad, nSizeTable);
}

// Read bitmap bits
file.Seek(g_Image.bf.bfOffBits, CFile::begin);

DWORD dwDataSize = WIDTHBYTES(pBIH->biWidth, pBIH->biBitCount) * labs(pBIH->biHeight);
file.Read(g_Image.data, dwDataSize);

file.Close(); cout << ".";
} cout << endl;
cout << "MFC: " << GetTickCount() - dwTick << " ms" << endl; return true;
}

解决方案 »

  1.   

    标准C++ 在VC中的表现真的很一般  其实最好的选择还是MFC
      

  2.   

    vc6对标准C++(或叫C++标准库)支持很差,如果你要使用标准C++,应该立刻放弃vc6
    vc7对标准C++的支持,无论是速度还是兼容性,都非常不错
    你可以同时安装vc6和vc7
      

  3.   

    切记:有利就有弊!
    集成开发工具IDE的功能越强,隐藏的东西越多,你了解的越少,学到的就少!
      

  4.   

    我亲自做过实验:用同样的代码在不同的环境下编译出Release模式的程序还是
    vc7快。另外还发现:在vc6中不支持类的模板函数(方法)在vc7中可以。
      

  5.   

    同意楼上几位的观点。正是认识到VC7的进步,才在这里苦恼了好几天,想要换成VC7,还需要公司同事一起换。今天决定去鼓动老大升到VC7,虽然感觉机会不大,因为他喜欢用C和API,换不换对他来讲没有多大必要。