C++ std::stringstream 操作优化
C++ std::stringstream operations optimizations
我的案例如下:
- 我有一个二进制文件,我正在使用 std::fstream 读取操作作为 (char*)
- 我的目标是从文件中取出十六进制格式的每个字节,然后将其附加到字符串变量
- 字符串变量应包含按照第 2 项格式化的文件的全部内容。
例如,假设我有以下二进制文件内容:
D0 46 98 57 A0 24 99 56 A3
我格式化每个字节的方式如下:
stringstream fin;;
for (size_t i = 0; i < fileb_size; ++i)
{
fin << hex << setfill('0') << setw(2) << static_cast<uint16_t>(fileb[i]);
}
// this would yield the output "D0469857A0249956A3"
return fin.str();
以上方法按预期工作,但是,我理解,对于大文件来说它非常慢; stringstream 用于输入格式!
我的问题是,是否有优化此类代码的方法或我正在采用的方法?我唯一的限制是输出应为字符串格式,如上所示。
谢谢。
std::stringstream
相当慢。它不会预分配,它总是涉及复制字符串,至少一次以检索它。也可以手动编码转换为十六进制以加快速度。
我认为像这样的东西可能会更高效:
// Quick and dirty
char to_hex(unsigned char nibble)
{
assert(nibble < 16);
if(nibble < 10)
return char('0' + nibble);
return char('A' + nibble - 10);
}
std::string to_hex(std::string const& filename)
{
// open file at end
std::ifstream ifs(filename, std::ios::binary|std::ios::ate);
// calculate file size and move to beginning
auto end = ifs.tellg();
ifs.seekg(0, std::ios::beg);
auto beg = ifs.tellg();
// preallocate the string
std::string out;
out.reserve((end - beg) * 2);
char buf[2048]; // larger = faster (within limits)
while(ifs.read(buf, sizeof(buf)) || ifs.gcount())
{
for(std::streamsize i = 0; i < ifs.gcount(); ++i)
{
out += to_hex(static_cast<unsigned char>(buf[i]) >> 4); // top nibble
out += to_hex(static_cast<unsigned char>(buf[i]) & 0b1111); // bottom nibble
}
}
return out;
}
它附加到预分配的字符串以尽量减少复制并避免重新分配。
我的案例如下:
- 我有一个二进制文件,我正在使用 std::fstream 读取操作作为 (char*)
- 我的目标是从文件中取出十六进制格式的每个字节,然后将其附加到字符串变量
- 字符串变量应包含按照第 2 项格式化的文件的全部内容。
例如,假设我有以下二进制文件内容:
D0 46 98 57 A0 24 99 56 A3
我格式化每个字节的方式如下:
stringstream fin;;
for (size_t i = 0; i < fileb_size; ++i)
{
fin << hex << setfill('0') << setw(2) << static_cast<uint16_t>(fileb[i]);
}
// this would yield the output "D0469857A0249956A3"
return fin.str();
以上方法按预期工作,但是,我理解,对于大文件来说它非常慢; stringstream 用于输入格式!
我的问题是,是否有优化此类代码的方法或我正在采用的方法?我唯一的限制是输出应为字符串格式,如上所示。
谢谢。
std::stringstream
相当慢。它不会预分配,它总是涉及复制字符串,至少一次以检索它。也可以手动编码转换为十六进制以加快速度。
我认为像这样的东西可能会更高效:
// Quick and dirty
char to_hex(unsigned char nibble)
{
assert(nibble < 16);
if(nibble < 10)
return char('0' + nibble);
return char('A' + nibble - 10);
}
std::string to_hex(std::string const& filename)
{
// open file at end
std::ifstream ifs(filename, std::ios::binary|std::ios::ate);
// calculate file size and move to beginning
auto end = ifs.tellg();
ifs.seekg(0, std::ios::beg);
auto beg = ifs.tellg();
// preallocate the string
std::string out;
out.reserve((end - beg) * 2);
char buf[2048]; // larger = faster (within limits)
while(ifs.read(buf, sizeof(buf)) || ifs.gcount())
{
for(std::streamsize i = 0; i < ifs.gcount(); ++i)
{
out += to_hex(static_cast<unsigned char>(buf[i]) >> 4); // top nibble
out += to_hex(static_cast<unsigned char>(buf[i]) & 0b1111); // bottom nibble
}
}
return out;
}
它附加到预分配的字符串以尽量减少复制并避免重新分配。