怎么样提高 ifstream 的效率
时间:2011-12-16
来源:互联网
{
ifstream is(GetFileName().c_str(),ifstream::in | ifstream::binary);
if(!is)
{
*error = "打开文件错误!";
return false;
}
TGAHEADER tgaHeader; // TGA file header(TGA文件头)
int sDepth; // Pixel depth;(像素深度)
// Read in header (binary)
// 读取文件头(二进制)
tgaHeader.identsize = is.get();
tgaHeader.colorMapType = is.get();
tgaHeader.imageType = is.get();
tgaHeader.colorMapStart = is.get() + (is.get() << 8);
tgaHeader.colorMapLength = is.get() + (is.get() << 8);
tgaHeader.colorMapBits = is.get();
tgaHeader.xstart = is.get() + (is.get() << 8);
tgaHeader.ystart = is.get() + (is.get() << 8);
tgaHeader.width = is.get() + (is.get() << 8);
tgaHeader.height = is.get() + (is.get() << 8);
tgaHeader.bits = is.get();
tgaHeader.descriptor = is.get();
// Get width, height, and depth of texture
// 获取纹理的宽度、高度和深度。
m_Width = tgaHeader.width;
m_Height = tgaHeader.height;
sDepth = tgaHeader.bits / 8;
// Put some validity checks here. Very simply, I only understand
// or care about 8, 24, or 32 bit targa's.
// 为此进行一些有效性验证。
// 非常简单,我只理解或关心8、24或32位的targa图像。
if(tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32)
{
*error = "targa图像位数错误!";
return false;
}
int lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
m_Bits = new GLbyte[lImageSize];
int i = 0;
for(;is && i < lImageSize ;++i)
{
m_Bits[i] = is.get();
}
if(i != lImageSize)
{
*error = "targa图像数据大小错误!";
return false;
}
// Set OpenGL format expected
// 设置期望的OpenGL格式
switch(sDepth)
{
case 3: // Most likely case(最可能出现的情况)
// 颜色以红绿蓝顺序表示
m_Format = GL_BGR_EXT;
m_Components = GL_RGB8;
break;
case 4:
// 颜色以红绿蓝、alpha顺序表示
m_Format = GL_BGRA_EXT;
m_Components = GL_RGBA8;
break;
case 1:
// 每个像素包含一个单一的亮度成分.
m_Format = GL_LUMINANCE;
m_Components = GL_LUMINANCE8;
break;
default:
*error = "OpenGL格式错误!";
return false;
};
return true;
}
bool Picture::LoadTGA::Load2(string* error)
{
ifstream is(GetFileName().c_str(),ifstream::in | ifstream::binary);
if(!is)
{
*error = "打开文件错误!";
return false;
}
TGAHEADER tgaHeader; // TGA file header(TGA文件头)
int sDepth; // Pixel depth;(像素深度)
// Read in header (binary)
// 读取文件头(二进制)
tgaHeader.identsize = is.get();
tgaHeader.colorMapType = is.get();
tgaHeader.imageType = is.get();
tgaHeader.colorMapStart = is.get() + (is.get() << 8);
tgaHeader.colorMapLength = is.get() + (is.get() << 8);
tgaHeader.colorMapBits = is.get();
tgaHeader.xstart = is.get() + (is.get() << 8);
tgaHeader.ystart = is.get() + (is.get() << 8);
tgaHeader.width = is.get() + (is.get() << 8);
tgaHeader.height = is.get() + (is.get() << 8);
tgaHeader.bits = is.get();
tgaHeader.descriptor = is.get();
// Get width, height, and depth of texture
// 获取纹理的宽度、高度和深度。
m_Width = tgaHeader.width;
m_Height = tgaHeader.height;
sDepth = tgaHeader.bits / 8;
// Put some validity checks here. Very simply, I only understand
// or care about 8, 24, or 32 bit targa's.
// 为此进行一些有效性验证。
// 非常简单,我只理解或关心8、24或32位的targa图像。
if(tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32)
{
*error = "targa图像位数错误!";
return false;
}
int lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
for(int i = 0;is && i < lImageSize ;++i)
{
m_Bit.push_back(is.get());
}
if(m_Bit.size() != lImageSize)
{
*error = "targa图像数据大小错误!";
return false;
}
// Set OpenGL format expected
// 设置期望的OpenGL格式
switch(sDepth)
{
case 3: // Most likely case(最可能出现的情况)
// 颜色以红绿蓝顺序表示
m_Format = GL_BGR_EXT;
m_Components = GL_RGB8;
break;
case 4:
// 颜色以红绿蓝、alpha顺序表示
m_Format = GL_BGRA_EXT;
m_Components = GL_RGBA8;
break;
case 1:
// 每个像素包含一个单一的亮度成分.
m_Format = GL_LUMINANCE;
m_Components = GL_LUMINANCE8;
break;
default:
*error = "OpenGL格式错误!";
return false;
};
return true;
}
bool Picture::LoadTGA::Load3(string* error)
{
FILE* pFile; // File pointer(文件指针)
TGAHEADER tgaHeader; // TGA file header(TGA文件头)
unsigned long lImageSize; // Size in bytes of image(文件的大小(以字节计))
short sDepth; // Pixel depth;(像素深度)
// Attempt to open the fil
// 试图打开文件。
fopen_s(&pFile,GetFileName().c_str(), "rb");
if(pFile == NULL)
{
*error = "打开文件错误!";
return false;
}
// Read in header (binary)
// 读取文件头(二进制)
fread(&tgaHeader, 18/* sizeof(TGAHEADER)*/, 1, pFile);
// Get width, height, and depth of texture
// 获取纹理的宽度、高度和深度。
m_Width = tgaHeader.width;
m_Height = tgaHeader.height;
sDepth = tgaHeader.bits / 8;
// Put some validity checks here. Very simply, I only understand
// or care about 8, 24, or 32 bit targa's.
// 为此进行一些有效性验证。
// 非常简单,我只理解或关心8、24或32位的targa图像。
if(tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32)
{
*error = "targa图像位数错误!";
return false;
}
// Calculate size of image buffer
// 计算图像缓冲区的大小。
lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
// Allocate memory and check for success
// 分配内存并检查分配是否成功
// pBits = (GLbyte*)malloc(lImageSize * sizeof(GLbyte));
m_Bits = new GLbyte[lImageSize];
if(m_Bits == NULL)
{
*error = "内存分配错误!";
return false;
}
// Read in the bits
// Check for read error. This should catch RLE or other
// weird formats that I don't want to recognize
// 读取数据位。
// 检查读取错误。这里应该捕捉RLE或其他我不知道的奇怪格式。
if(fread(m_Bits, lImageSize, 1, pFile) != 1)
{
*error = "读取数据位错误!";
// free(pBits);
return false;
}
// Set OpenGL format expected
// 设置期望的OpenGL格式
switch(sDepth)
{
case 3: // Most likely case(最可能出现的情况)
// 颜色以红绿蓝顺序表示
m_Format = GL_BGR_EXT;
m_Components = GL_RGB8;
break;
case 4:
// 颜色以红绿蓝、alpha顺序表示
m_Format = GL_BGRA_EXT;
m_Components = GL_RGBA8;
break;
case 1:
// 每个像素包含一个单一的亮度成分.
m_Format = GL_LUMINANCE;
m_Components = GL_LUMINANCE8;
break;
default:
*error = "OpenGL格式错误!";
return false;
};
// Done with File
// 完成对文件的操作
fclose(pFile);
return true;
}
我用ctime 中的clock()函数计算上面三个函数在指定时间的运行次数。
分别是ifstream + vector 13,ifstream + 数组 16,fopen_s + fread 2036
效率差的也太多了吧。
我想一定是我的编程方式有问题。希望高手告诉 一下我提高效率的方法。
因为我没学过C语言,所以不清楚 fopen_s、fread函数的用法。
一直习惯用iostream。虽然我知道C函数会比iostream快。。。但从没想过会快这么多。
难道这就是事实?
作者: pwy198407 发布时间: 2011-12-16
作者: masm32v9 发布时间: 2011-12-16
作者: mingliang1212 发布时间: 2011-12-16
作者: gykgod 发布时间: 2011-12-16
使用FILE *
作者: zhao4zhong1 发布时间: 2011-12-16
作者: luciferisnotsatan 发布时间: 2011-12-16
C/C++ code
is.read(&StructObj,Len);
作者: zm19870528 发布时间: 2011-12-16
使用is.read可以达到使用fread的2/3。
不过看来大家说的没错,摒弃fstream是最好的选择。
作者: pwy198407 发布时间: 2011-12-16
所以特别是对低层的操作,C效率的确要比C++高很多(这也是很多低层用C不用C++的原因)
对于文件操作,如果要考虑效率的话,的确可以放弃fstream,用FILE
FILE操作文件也很简单的,LZ说没学过C,事实上C是C++的一个子集,会用C++,不可能不会用C的
只是只要熟悉一下函数就可以了
作者: keiy 发布时间: 2011-12-16
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28