需要有关标准库的高效文件i / o的建议 [英] Need advices on efficient file i/o with standard library
问题描述
我正在编写一个c ++程序,它有许多(100+)个线程读/写文件
。如果不考虑效率,它的效果很好。
文件i / o似乎是瓶颈。
这是我读取和写入文件的代码:
>
#include< fstream>
#include< sstream>
#include< string>
使用namespace std;
bool write(const string& path,const string& contents,
ios :: openmode mode)
{
ofstream out;
bool status;
out.open(path.c_str(),mode );
if(!out.fail()){
out<<内容;
}
status =!out.fail();
out.close();
返回状态;
}
bool read(const string& path,string& contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear( );
in.open(path.c_str(),ios :: in);
if(in){
ss< < in.rdbuf();
contents = ss.str();
}
status =!in.fail();
in.close();
返回状态;
}
我有关于如何优化我的代码的一些线索,任何方向都将是非常感谢。
I''m writing a c++ program that has many (100+) threads read/write files
simultaneously. It works well if not considering the efficiency. The
file i/o seems to be the bottleneck.
This is my code to read from and write to files:
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
bool write(const string &path, const string &contents,
ios::openmode mode)
{
ofstream out;
bool status;
out.open(path.c_str(), mode);
if (!out.fail()) {
out << contents;
}
status = !out.fail();
out.close();
return status;
}
bool read(const string &path, string &contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(), ios::in);
if (in) {
ss << in.rdbuf();
contents = ss.str();
}
status = !in.fail();
in.close();
return status;
}
I have few clues about how to optimize my code, any direction would be
greatly appreciated.
推荐答案
bool read(const string& path,string& contents)
bool read(const string &path, string &contents)
{
ifstream in;
stringstream ss;
布尔状态;
contents.clear();
in.open(path.c_str(),ios :: in);
if(in){
ss<< in.rdbuf();
contents = ss.str();
}
status =!in.fail();
in.close();
返回状态;
}
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(), ios::in);
if (in) {
ss << in.rdbuf();
contents = ss.str();
}
status = !in.fail();
in.close();
return status;
}
我只是尝试使用fopen(),fread(),fclose()来读取文件内容:
bool read(const string& path,string& contents)
{
FILE * fp;
char buf [2048];
fp = fopen(path.c_str( ),r;;
if(fp){
while(fread(buf,2048,1,fp)){
内容+ = buf;
}
fclose(fp);
返回true;
} else {
返回false;
}
}
这比(字面意思)快4倍以前的C ++版本。是否可以让C ++版本接近这个速度?
I just tried to use fopen(), fread(), fclose() to read file contents:
bool read(const string &path, string &contents)
{
FILE *fp;
char buf[2048];
fp = fopen(path.c_str(), "r");
if (fp) {
while (fread(buf, 2048, 1, fp)) {
contents += buf;
}
fclose(fp);
return true;
} else {
return false;
}
}
This runs 4 times faster (literally) than the previous C++ version. Is
it possible to get the C++ version close to this speed?
Gan Quan写道:
Gan Quan wrote:
> bool read(const string& path,string& contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(),ios :: in);
if( in){
ss<< in.rdbuf();
contents = ss.str();
}
status =!in.fail();
in.close();
返回状态;
}
>bool read(const string &path, string &contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(), ios::in);
if (in) {
ss << in.rdbuf();
contents = ss.str();
}
status = !in.fail();
in.close();
return status;
}
我只是尝试使用fopen(),fread(),fclose()来读取文件内容: br />
bool read(const string& path,string& contents)
{
FILE * fp;
char buf [2048];
fp = fopen(path.c_str()," r");
if(fp){
while(fread(buf,2048,1,fp)){
content + = buf;
}
fclose (fp);
返回true;
}否则{
返回false;
}
}
这比前一个C ++版本快4倍(字面意思)。
有可能让C ++版本接近这个速度吗?
I just tried to use fopen(), fread(), fclose() to read file contents:
bool read(const string &path, string &contents)
{
FILE *fp;
char buf[2048];
fp = fopen(path.c_str(), "r");
if (fp) {
while (fread(buf, 2048, 1, fp)) {
contents += buf;
}
fclose(fp);
return true;
} else {
return false;
}
}
This runs 4 times faster (literally) than the previous C++ version. Is
it possible to get the C++ version close to this speed?
我不知道速度,但你可以试试:
#include< iterator>
#include< iostream>
#include< fstream>
#include< string>
#include < iosfwd>
bool read_1(std :: string const& path,
std :: string& contents)
{
std :: ifstream in;
bool status;
in.open(path.c_str(),std :: ios :: in);
if(in){
std :: string buffer(std :: istreambuf_iterator< char>(in),
(std :: istreambuf_iterator< char>()));
contents.swap(缓冲区);
}
status =!in .fail();
in.close();
返回状态;
}
至少,这可以避免绕过弦流。至于
表现,你只需要衡量。但我认为,你已经建立了一个框架来实现这一目标。我会对
比较感兴趣。
Best
Kai-Uwe Bux
I don''t know about speed, but you could try:
#include <iterator>
#include <iostream>
#include <fstream>
#include <string>
#include <iosfwd>
bool read_1 ( std::string const & path,
std::string & contents )
{
std::ifstream in;
bool status;
in.open( path.c_str(), std::ios::in );
if ( in ) {
std::string buffer ( std::istreambuf_iterator<char>( in ),
(std::istreambuf_iterator<char>()) );
contents.swap( buffer );
}
status = !in.fail();
in.close();
return status;
}
At least, this avoids the detour through a stringstream. As for the
performance, you will just have to measure. But I take it, that you have
already a framework in place for doing that. I would be interested in the
comparison.
Best
Kai-Uwe Bux
< br>
?周四,2006年10月12日14:16:41 + 0800£?甘泉< vi ****** @gmail.comD'μà:
?ú Thu, 12 Oct 2006 14:16:41 +0800£?Gan Quan <vi******@gmail.comD′μà:
> bool read(const string& path,string& contents)
{
ifstream in;
stringstream ss;
bool状态;
content.clear();
in.open(path.c_str(),ios :: in);
if(in){
ss<< in.rdbuf();
contents = ss.str();
}
status =!in.fail();
in.close();
返回状态;
}
>bool read(const string &path, string &contents)
{
ifstream in;
stringstream ss;
bool status;
contents.clear();
in.open(path.c_str(), ios::in);
if (in) {
ss << in.rdbuf();
contents = ss.str();
}
status = !in.fail();
in.close();
return status;
}
我只是尝试使用fopen(),fread(),fclose()来读取文件内容: br />
bool read(const string& path,string& contents)
{
FILE * fp;
char buf [2048];
fp = fopen(path.c_str()," r");
if(fp){
while(fread(buf,2048,1,fp)){
content + = buf;
}
fclose (fp);
返回true;
}否则{
返回false;
}
}
这比前一个C ++版本快4倍(字面意思)。
是否可以让C ++版本接近这个速度?
I just tried to use fopen(), fread(), fclose() to read file contents:
bool read(const string &path, string &contents)
{
FILE *fp;
char buf[2048];
fp = fopen(path.c_str(), "r");
if (fp) {
while (fread(buf, 2048, 1, fp)) {
contents += buf;
}
fclose(fp);
return true;
} else {
return false;
}
}
This runs 4 times faster (literally) than the previous C++ version. Is
it possible to get the C++ version close to this speed?
你应该减少磁盘操作的性能,
每个文件
,你可以先计算它的大小,然后用一个大的缓冲区来计算
足以在一次读取操作中获取数据。
- -
问候
you should reduce the disk operation for performance,
for each file, you can caculate its size first and malloc a buffer big
enough to get the data in one read operation.
--
Regards
这篇关于需要有关标准库的高效文件i / o的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!