 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gan Quan Guest
|
Posted: Thu Oct 12, 2006 9:10 am Post subject: Need advices on efficient file i/o with standard library |
|
|
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. |
|
| Back to top |
|
 |
Gan Quan Guest
|
Posted: Thu Oct 12, 2006 9:10 am Post subject: Re: Need advices on efficient file i/o with standard library |
|
|
| Quote: | 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 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? |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Thu Oct 12, 2006 9:10 am Post subject: Re: Need advices on efficient file i/o with standard library |
|
|
Gan Quan wrote:
| Quote: | 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 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?
|
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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|