Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ifstream is too slow !!!

1,061 views
Skip to first unread message

XueLiang

unread,
Aug 30, 2001, 12:47:09 PM8/30/01
to
Hello all
 
when I read a big file nearly 40M with ifstream, it is terribly slow;
 
how can I accelerate the reading speed of my program?
 
maybe I should use Memory Mapping File & Windows API instead ?
 
thanks in advance!
 
ifstream in;
in..open ("C:\\bigfile.txt", ios::in|ios::binary);
while (in.good)
{
    in.getline(pFileBuf, SIZE_OF_BUF);
    cout<<pFileBuf;
}

P.J. Plauger

unread,
Aug 30, 2001, 2:04:58 PM8/30/01
to
"XueLiang" <z...@ustc.edu> wrote in message news:#FeC3LXMBHA.1428@tkmsftngp05...

> when I read a big file nearly 40M with ifstream, it is terribly slow;
>
> how can I accelerate the reading speed of my program?

See http://www.dinkumware.com/vc_fixes.html.

There's a one-line fix to fstream that should give you a significant
speed up.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Erik Funkenbusch

unread,
Aug 30, 2001, 3:20:28 PM8/30/01
to
While there is a fix that will speed things up a bit, you should realize
that writing to cout will slow things down as well.

"XueLiang" <z...@ustc.edu> wrote in message
news:#FeC3LXMBHA.1428@tkmsftngp05...

Mike Mozhaev

unread,
Aug 31, 2001, 2:00:26 AM8/31/01
to
"XueLiang" <z...@ustc.edu> wrote in message news:<#FeC3LXMBHA.1428@tkmsftngp05>...
> when I read a big file nearly 40M with ifstream, it is terribly slow;
> how can I accelerate the reading speed of my program?
> maybe I should use Memory Mapping File & Windows API instead ?
>
> ifstream in;
> in..open ("C:\\bigfile.txt", ios::in|ios::binary);
> while (in.good)
> {
> in.getline(pFileBuf, SIZE OF BUF);
> cout<<pFileBuf;
> }

Do you really need "getline" or "read" would be enough.
getline is much slower, because it parses input for "end of line".

Some sample:

#include <fstream>

using std::ios;

int main()
{
const int size = 10000;
char *pFileBuf = new char[size];

std::ifstream fin("bigfile.txt", ios::in|ios::binary);

int n;
while (fin.read(pFileBuf, size), n=fin.gcount())
{
// do whatever you want with data
}

fin.close();

delete [] pFileBuf;

return 0;
}

Mike.

Tom

unread,
Aug 31, 2001, 12:43:44 PM8/31/01
to
mike_m...@yahoo.com (Mike Mozhaev) wrote in message news:<e23bf68c.01083...@posting.google.com>...

> "XueLiang" <z...@ustc.edu> wrote in message news:<#FeC3LXMBHA.1428@tkmsftngp05>...
> > when I read a big file nearly 40M with ifstream, it is terribly slow;
> > how can I accelerate the reading speed of my program?
> > maybe I should use Memory Mapping File & Windows API instead ?
> >
> > ifstream in;
> > in..open ("C:\\bigfile.txt", ios::in|ios::binary);
> > while (in.good)
> > {
> > in.getline(pFileBuf, SIZE OF BUF);
> > cout<<pFileBuf;
> > }
>
> Do you really need "getline" or "read" would be enough.
> getline is much slower, because it parses input for "end of line".

If you really need performance (say in an inner loop), you should
access the streambuf directly.

std::streamsize num;
while ((num = fin.rdbuf()->sgetn(pFilebuf, size)) > 0)
{
//blah with num characters
}

Tom

0 new messages