Forum Topic: C++ fastest way to read iostream?

(185 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

Alphabit

Reply To Post Reply & Quote

Posted at: 10/28/09 10:45 PM

Alphabit NEUTRAL LEVEL 09

Sign-Up: 02/14/06

Posts: 4,066

I have an iostream object and I need to read from it as fast as possible...
I'll have to break up the data into words (std::string) separated by certain characters... Currently, I'm just using stream.get(char) and fetching characters one at a time... I have to parse a file that contains over 800,000 characters and I have to try to get my algorithm to work under 5 seconds but I currently get about 9 seconds... What can I do to speed things up? I'm not too familiar with the iostream library.


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/28/09 10:53 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,053

std::string buffer
streamin >> buffer;

will extract one word from the stream and place it into buffer, (words being delimited by whitespace)

std::string buffer
while(!streamin.eof()){
streamin >> buffer;
doStuffWith(buffer);
}


None

thoughtpolice

Reply To Post Reply & Quote

Posted at: 10/28/09 11:09 PM

thoughtpolice DARK LEVEL 10

Sign-Up: 03/24/03

Posts: 3,101

Use a profiler? Use a library that is designed to be highly performant for text processing?

Of course, it brings into question whether you really need to go from 9 seconds to 5 seconds - if you have to do a lot of contortions it might not be worth it, but that's a decision that's dependent on a lot of factors.

omg.
Playstation Network tag: muffin-noodle
the empty set


None

DougyTheFreshmaker

Reply To Post Reply & Quote

Posted at: 10/29/09 03:48 AM

DougyTheFreshmaker NEUTRAL LEVEL 02

Sign-Up: 07/30/07

Posts: 519

At 10/28/09 10:45 PM, Alphabit wrote: I have an iostream object and I need to read from it as fast as possible...
I have to parse a file that contains over 800,000 characters and I have to try to get my algorithm to work under 5 seconds but I currently get about 9 seconds...

File I/O kills performance. You should read the data in large "chunks" to avoid accessing the file as much as possible.

It sounds like you may understand that already, however, from your original question ('one character at a time...'). If that is the case, then Google should suffice. This C++ ifstream Reference is sure to offer something useful. A quick glance at the read function looks promising.

We should take care not to make the intellect our god; it has, of course, powerful muscles, but no personality.
Freshmaking
Brainscrape

BBS Signature

None

CronoMan

Reply To Post Reply & Quote

Posted at: 10/29/09 09:40 AM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,984

My advice would be to read the entire file into memory, and then parse from there.
I believe it should be plausible to get the time down to under a second
If you read the entire file, you make the hardware work in your favor. After all, the harddrive + our friend the DMA controller, can deliver 50MB per second, and your file is 800k?
Going through a linear char array of 800 000 elements should be fairly straightforward

At least in my spectacularly brilliant mind

"no sound in ass"


None

Alphabit

Reply To Post Reply & Quote

Posted at: 10/30/09 03:58 AM

Alphabit NEUTRAL LEVEL 09

Sign-Up: 02/14/06

Posts: 4,066

Thanks guys.
The string buffer thing did give a slight performance boost.


None

littleMonsterGames

Reply To Post Reply & Quote

Posted at: 10/31/09 05:01 PM

littleMonsterGames DARK LEVEL 05

Sign-Up: 12/24/08

Posts: 340

At 10/29/09 09:40 AM, CronoMan wrote: My advice would be to read the entire file into memory, and then parse from there.

That's what he doesn't want to do, I think.

The LittleMonsterGames website: http://www.littlemonstergames.com - super fun, I promise :)

BBS Signature

None

kiwi-kiwi

Reply To Post Reply & Quote

Posted at: 10/31/09 06:04 PM

kiwi-kiwi LIGHT LEVEL 08

Sign-Up: 03/06/09

Posts: 646

At 10/31/09 05:01 PM, littleMonsterGames wrote:
At 10/29/09 09:40 AM, CronoMan wrote: My advice would be to read the entire file into memory, and then parse from there.
That's what he doesn't want to do, I think.

CronoMan is right.
Also if you need speed I'd advise you to use fread.
Relevant pic:

C++ fastest way to read iostream?


All times are Eastern Standard Time (GMT -5) | Current Time: 10:04 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!